1

When button clicked, i must update a TextView in same position and I have done it, but 9th and 10th position of RecyclerView follow first position and second position. In other word, if I clicked first button position, First position of TextView is updated, but, 9th position of TextView also updated, It should be not updated. How to solve this?

I follow this link

here is my Adapter

class ProductsByStoreAdapter extends RecyclerView.Adapter<ProductsByStoreAdapter.ViewHolder> {

private ArrayList<Products> products;

 ProductsByStoreAdapter(ArrayList<Products> productses) {
     this.products = productses;
     //products = CenterRepository.getCenterRepository()
             //.getListOfProductsInShoppingList();
}

@Override
public ProductsByStoreAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.products_card_item, viewGroup, false);
    return new ProductsByStoreAdapter.ViewHolder(view);
}

class ViewHolder extends RecyclerView.ViewHolder{
    private TextView tv_product_name, tv_product_price, tv_product_quantity;
    private ImageView im_product_image;
    private ImageButton button_add_product, button_min_product;
    private EditText e_note;
    private LinearLayout layout_note;
    ViewHolder(View view) {
        super(view);

        im_product_image            = (ImageView)view.findViewById(R.id.product_image);
        tv_product_name             = (TextView)view.findViewById(R.id.product_name);
        tv_product_price            = (TextView)view.findViewById(R.id.product_price);
        tv_product_quantity         = (TextView)view.findViewById(R.id.product_quantity);
        e_note                      = (EditText)view.findViewById(R.id.e_note);
        layout_note                 = (LinearLayout)view.findViewById(R.id.layout_note);

        this.button_add_product = (ImageButton)view.findViewById(R.id.button_add_product);
        button_min_product = (ImageButton)view.findViewById(R.id.button_min_product);

    }
}

@Override
public void onBindViewHolder(final ProductsByStoreAdapter.ViewHolder viewHolder, final int position) {
    Glide.with(viewHolder.im_product_image.getContext())
            .load(products.get(position).getImage_uri())
            .centerCrop()
            .crossFade()
            //.placeholder(R.drawable.placeholder_main)
            .into(viewHolder.im_product_image);
    CurrencyFormats currencyFormat = new CurrencyFormats();
    viewHolder.tv_product_name.setText(products.get(position).getName());
    viewHolder.tv_product_price.setText(currencyFormat.toRupiah(products.get(position).getPrice()));
    //viewHolder.tv_product_quantity.setText("0");
    viewHolder.button_add_product.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //current object
            Products tempObj = (products).get(position);
         ((ProductsByStoreActivity)view.getContext()).updateItemCount(true);
                tempObj.setQuantity(String.valueOf(1));
          viewHolder.tv_product_quantity.setText(tempObj.getQuantity());
        }
    });
    viewHolder.button_min_product.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Products tempObj = (products).get(position);
                                 viewHolder.tv_product_quantity.setText(CenterRepository
                            .getCenterRepository().getListOfProductsInShoppingList()
                            .get(indexOfTempInShopingList).getQuantity());
                   
                        ((ProductsByStoreActivity)view.getContext()).updateItemCount(false);
                    }
                }
            }else {

            }
        }
    });

}

@Override
public int getItemCount() {
    //return products.size();
    return products == null ? 0 : products.size();
}}
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Latief Anwar
  • 1,833
  • 17
  • 27

3 Answers3

1

You need to move your onclick listener into onCreateViewHolder.

final ProductsByStoreAdapter.ViewHolder viewHolder = new ProductsByStoreAdapter.ViewHolder(view);
button_add_product.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        //current object
        Products tempObj = products.get(viewHolder.getAdapterPosition(););
     ((ProductsByStoreActivity)view.getContext()).updateItemCount(true);
            tempObj.setQuantity(String.valueOf(1));
      viewHolder.tv_product_quantity.setText(tempObj.getQuantity());
    }
});
return viewHolder;

You can do the same with the other onclicklistener

AbdulAli
  • 555
  • 3
  • 9
0

Update: You do not setText to your product_quantity textview in the BindView function, unless a button is clicked. this means its value will be recycled from other items. you should check with an if statement what is the quantity of the item and present it even without clicking.

Old and not correct answer: I am not sure if this is the problem, but its an easy check, so try it out. There are 2 positions - the adapter position, and the layout position. I think maybe the position you are using (the one that came from the onBind function) is the latter. You want the adapter position, so try using getAdapterPosition() like this: Products tempObj = (products).get(getAdapterPosition());

0

add below line to resolve the problem of 9th and 10th position of item

@Override
public int getItemViewType(int position) 
{
    return position;
}
Zoe
  • 27,060
  • 21
  • 118
  • 148