RecyclerView doesn't display all items in list after update android studio

Trung :

I want to show these items inside my RecyclerView but it doesn't show at all and I can't see the error. Maybe you guys can help me out. My code here:

The Activity:

public class ProductsViewActivity extends AppCompatActivity {
    private RecyclerView productView;
    private ArrayList<Product> lstProduct;
    private RecyclerView.LayoutManager layoutManager;
    private ProductAdapter productAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_products_view);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        lstProduct = new ArrayList<>();
        lstProduct.add(new Product(1, "Product 1", "This is good product", "https://www.dollargeneral.com/media/catalog/product/cache/image/700x700/e9c3970ab036de70892d86c6d221abfe/2/0/20706301_kibbles_nbitsbacon_steakflavordrydogfood3.6lb_main.jpg", 34.78));
        lstProduct.add(new Product(2, "Product 2", "This is good product", "https://www.iams.com/images/default-source/product-packshot/dog/iams-proactive-health-smart-puppy-large-breed-dog-food.png?sfvrsn=6", 34.78));
        productView = findViewById(R.id.productView);
        loadProductView();
    }

    private void loadProductView() {
        productView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);
        productView.setLayoutManager(layoutManager);

        productAdapter = new ProductAdapter(lstProduct);
        productView.setAdapter(productAdapter);
        Log.e("Product view", "Size ======>" + productAdapter.getItemCount());
    }
}

The Adapter class:

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.RecycleViewHolder> {

    public static class RecycleViewHolder extends RecyclerView.ViewHolder {

        private TextView tvName;
        private TextView tvDescription;
        private TextView tvPrice;
        private TextView tvImage;

        public RecycleViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            tvDescription = itemView.findViewById(R.id.tvDescription);
            tvPrice = itemView.findViewById(R.id.tvPrice);
            tvImage = itemView.findViewById(R.id.tvImage);
        }
    }

    private List<Product> lstProduct;

    public ProductAdapter(List<Product> lstProduct) {
        this.lstProduct = lstProduct;
    }

    @NonNull
    @Override
    public RecycleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.product_list_item, parent, false);

        RecycleViewHolder viewHolder = new RecycleViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecycleViewHolder holder, int position) {
        holder.tvName.setText(lstProduct.get(position).getName());
        holder.tvDescription.setText(lstProduct.get(position).getDescription());
        holder.tvPrice.setText(String.valueOf(lstProduct.get(position).getPrice()));
        holder.tvImage.setText(String.valueOf(lstProduct.get(position).getImgURL()));

    }

    @Override
    public int getItemCount() {
        return lstProduct.size();
    }
}

The Product class:

public class Product implements Serializable {

    private int id;
    private String name;
    private String description;
    private String imgURL;
    private double price;

    public Product() {
    }

    public Product(int id, String name, String description, String imgURL, double price) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.imgURL = imgURL;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImgURL() {
        return imgURL;
    }

    public void setImgURL(String imgURL) {
        this.imgURL = imgURL;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}
med.Hamdan :

check if "product_list_item" root ViewGroup width is : wrap_content not match_parent.

layou/product_list_item.xml:

<LinearLayout 
     android:layout_height="wrap_content"

of course you encounter this problem if it was match_parent.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=152380&siteId=1