Bhargavi Abhirami
Bhargavi Abhirami

Reputation: 65

checking a checkbox in recyclerview making other random checkboxes checked too android

I have one recyclerview adapter which has a checkbox.. when i try to check one box recyclerview makes random checkboxes checked too.. Actually i found the error its because all checkboxes have one state and reuses the view.. and also i have tried many options posted in stackoverflow but nothing worked for me..

This below am checking in the table if there is any item then that particular item checkbox should be checked..

if (Objects.equals(orderTable.getItemCode(), items.getItemCode()) && Objects.equals(orderTable.getStoreId(), storesPojos.getId()))
{
    /*
     * Log.e(TAG+" Order Table Code", String.valueOf(orderTable.getItemCode()));
     * Log.e(TAG+" ItemsPojo Code", String.valueOf(items.getItemCode()));
     *
     * Log.e(TAG +" Order Table ID",orderTable.getStoreId());
     * Log.e(TAG+" store ID",orderTable.getStoreId());
     */

    Log.e("Position of Adapter///", String.valueOf(position));
    Log.e("SelectedPosition of Adapter///", String.valueOf(selectedPosition));
    if (selectedPosition == position) {
        holder.checkBox.setChecked(true);
    }
    else {
        holder.checkBox.setChecked(false);
    }
    checked++;
    noOfItems.setText(String.valueOf(checked));
}

This below code is my checkbox.setOnCheckedChangeListener()

holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        ItemsPojo itemsPojo = itemList.get(position);

        if (selectedPosition == position)
        {
            checked++;
            noOfItems.setText(String.valueOf(checked));
            dbHelper.insertItemFromOrderTable(itemsPojo.getItemCode());
        }
        else
        {
            checked--;
            noOfItems.setText(String.valueOf(checked));
            dbHelper.deleteItemFromOrderTable(itemsPojo.getItemCode());
        }

Here is my onBindViewHolder()

@Override
public void onBindViewHolder(final ItemsAdapter.MyViewHolder holder, final int position) {
    Log.i("Position of Adapter///", String.valueOf(position));
    items = itemsPojos.get(position);
    selectedPosition = position;

    holder.product.setText(items.getProduct());
    holder.desc.setText(items.getSubCategory());

    if (orderTables.size() != 0) {
        Log.e("Order Table Size ", String.valueOf(orderTables.size()));

        for (OrderTable orderTable : orderTables) {
            if (Objects.equals(orderTable.getItemCode(), items.getItemCode()) && Objects.equals(orderTable.getStoreId(), storesPojos.getId()))
            {
                /*   
                * Log.e(TAG+" Order Table Code", String.valueOf(orderTable.getItemCode()));
                * Log.e(TAG+" ItemsPojo Code", String.valueOf(items.getItemCode()));
                *
                * Log.e(TAG +" Order Table ID",orderTable.getStoreId());
                * Log.e(TAG+" store ID",orderTable.getStoreId());
                */

                Log.e("Position of Adapter///", String.valueOf(position));
                Log.e("SelectedPosition of Adapter///", String.valueOf(selectedPosition));

                if (selectedPosition == position) {
                    holder.checkBox.setChecked(true);
                }
                else {
                    holder.checkBox.setChecked(false);
                }

                checked++;
                noOfItems.setText(String.valueOf(checked));
            }                 
        }
    }

Upvotes: 0

Views: 467

Answers (1)

RobCo
RobCo

Reputation: 6495

You cannot trust views in your recycler to hold the state of your data.

When the page is scrolled and a row is recycled, the view is just used for a different item. So if the old item was checked, it now suddenly appears the new 'random' item is also checked. You may also see when you scroll back that the item you previously checked is in fact no longer checked.

To solve this you must keep the checked state in your data set, for example in your POJO that represents one item. Then when binding that to a view in your adapter you set the checked state of the CheckBox according to that pojo state.

Upvotes: 0

Related Questions