0

I have a recyclerView in which i have a list of items displayed in a LinearLayout.There is a "increase" button in every list which will increment a quantity by "1".But when I click the button on the first list item to increment the number..the incremented value is displayed in the last list item of the recyclerView not in the desired position where i clicked.Can anyone help me find the solution?

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    myholder = holder;
    holder.item_name_text.setText(data.get(position).getName());
    holder.item_price_text.setText(data.get(position).getPrice().toString());
    holder.item_quantity_text.setText("500");

    //Adding item to the cart
    holder.add_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(totalItem>=0 && totalItem<10){
                totalItem++;

                myholder.item_totalquantity_text.setText(String.valueOf(totalItem));
            }else{
                Toast.makeText(myholder.itemView.getContext(),"Cannot add more item",Toast.LENGTH_SHORT).show();
            }
        }
    });
curiousMind
  • 2,812
  • 1
  • 17
  • 38
Sagar Suri
  • 15
  • 4

2 Answers2

0

Do not set OnClickListener() in onBindViewHolder(). Instead do it in your ViewHolder class itself. And main ly as RecyclerView re-uses the holder objects to represent the new set of data that is becoming visible. So the listener on which yo click might be belonging to some other view holder so it appears there instead.

Have a quick read of this and this. These are not very relevant to you but it has info to understnad recycling concept so you can fix your stuff easily.

Community
  • 1
  • 1
cgr
  • 4,578
  • 2
  • 28
  • 52
  • So according to the suggestions you have given I guess I wont be able to use recyclerView to achieve the desired results what i am expecting. – Sagar Suri Dec 07 '15 at 14:13
  • You can do. But do not call setOnclickListener in ViewHolder itself. You must have something like this: public static class VHFriends extends RecyclerView.ViewHolder implements View.OnClickListener { public ImageView addImage; public VHFriends(View itemView) { .. addImage = findViewByID(); addImage.setOnClickListener(new .....); } } – cgr Dec 07 '15 at 14:18
  • ok I will surely update the code.Yeah you are right I have a custom viewholder in the adapter. – Sagar Suri Dec 08 '15 at 05:08
0

So I always think of recyclerviews as the frontend that displays my data. If you would like to make changes to the actual data itself and make sure it gets reflected in the recyclerview, you will need to ensure the changes are made inside the arraylist of data objects.

You will need to add a field inside the data object and call it counter. Then you must increment the counter once the user clicks on the onClickListener.

myholder.item_totalquantity_text.setText(data.get(position).setCounter(data.get(position).getCounter())+1);
Simon
  • 19,658
  • 27
  • 149
  • 217
  • That is a great point to be noted.But what if i don't want to store a counter in the counter variable in the data object.Is there any other workaround for it. – Sagar Suri Dec 08 '15 at 05:00
  • Unfortunately not. As I mentioned, the recyclerview is only for presentation only and it will present whatever you feed it in terms of the data objects. You will need to create the counter inside the data object to keep track so that the recyclerview can read from it and display properly. – Simon Dec 08 '15 at 05:06
  • Ok thanks bro.I will look after it.You were a great help. – Sagar Suri Dec 08 '15 at 05:11