0

I created a RecyclerView Adapter that shows a list of items and next to each on of them there is a Checkbox.

Inside the adapter I created:

interface OnItemCheckListener {
    void onItemCheck(DiscoverBooks books);
    void onItemUncheck(DiscoverBooks books);
}

@NonNull
private OnItemCheckListener onItemCheckListener;


@Override
public void onBindViewHolder(BorrowViewHolder holder, int position) {
    holder.bind(items.get(position), position);

    final Discoveritems currentItem = items.get(position);

    holder.setOnClickListener( v -> {
        holder.Cb_Borrow.setChecked( !holder.Cb_Borrow.isChecked());
        if (holder.Cb_Borrow.isChecked()) {
            onItemCheckListener.onItemCheck(currentItem);
        } else {
            onItemCheckListener.onItemUncheck(currentItem);
        }
    } );

}

Where I give the option for the user to check the Checkbox and to use this data later.

enter image description here

However, I found that if for example, I clicked the Checkbox of the first item, it makes every 7 item to be checked.

For example what I get:

item 1 - item checked
item 2
item 3
item 4
item 5
item 6
item 7 
item 8 - item checked
item 9
item 10
item 11
item 12
item 13
item 14 
item 15 - item checked

Any idea why it happens?

Thank you

Ben
  • 1,737
  • 2
  • 30
  • 61
  • 1
    `ViewHolder`s do not hold any data, only represent it. Your `holder.bind` method must reflect current state of item being checked because you're experiencing view recycling in action - view that was checked is reused for item that is not. – Pawel Jul 26 '20 at 21:43
  • Does this answer your question? [Duplication in Checkbox selection in RecyclerView](https://stackoverflow.com/questions/46951014/duplication-in-checkbox-selection-in-recyclerview) – Uuu Uuu Jul 27 '20 at 01:58
  • I tried the solutions but the options they offer as holder.Cb_Borrow.setChecked(currentItem.isSelected()); doesnt work since I get error in isSelected. – Ben Jul 28 '20 at 17:45

0 Answers0