Recycler view check box unticks itself when adding a new item

Viewed 10

I've created a recycler view adapater that takes items from a list based on their position. At the current moment, when I try to add a new item it currently unticks some items, but not others (those that are beyond position 7). My original code was:

public void onBindViewHolder(ViewHolder holder, int position){
        db.openDatabase();

        ShoppingModel item = shoppingList.get(position);
        holder.item.setText(item.getItem());
        holder.item.setChecked(toBoolean(item.getStatus()));
        if(holder.item.isChecked()){
            holder.item.setTextColor(Color.GRAY);
        }

        holder.item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked){
                    db.updateStatus(item.getId(), 1);
                }
                else{
                    db.updateStatus(item.getId(), 0);
                }
            }
        });

I thought it was then I was not updating the shoppingList or the item so I added the following code to get:

public void onBindViewHolder(ViewHolder holder, int position){
        db.openDatabase();

        ShoppingModel item = shoppingList.get(position);
        holder.item.setText(item.getItem());
        holder.item.setChecked(toBoolean(item.getStatus()));
        if(holder.item.isChecked()){
            holder.item.setTextColor(Color.GRAY);
        }

        holder.item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked){
                    db.updateStatus(item.getId(), 1);
                    holder.item.setTextColor(Color.GRAY);
                    shoppingList.get(holder.getAbsoluteAdapterPosition()).setStatus(1);
                    holder.item.setChecked(true);
                }
                else{
                    db.updateStatus(item.getId(), 0);
                    holder.item.setTextColor(Color.BLACK);
                    shoppingList.get(holder.getAbsoluteAdapterPosition()).setStatus(0);
                    holder.item.setChecked(false);
                }
            }
        });

But the issue of unticking the box remains. You can find the full code https://github.com/PhilipDW183/ShoppingListApp if you need. Any ideas would be appreciated, or any guidance!

Thanks in advance!

1 Answers

Not sure why but this seems to have resolved it:

private int status;


holder.item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked){
                    status = 1;
                    holder.item.setTextColor(Color.GRAY);
                } else {
                    status = 0;
                    holder.item.setTextColor(Color.BLACK);
                }
                updateItem(holder.getAbsoluteAdapterPosition(), status);
            }
        });


public void updateItem(int position, int status){
        ShoppingModel item = shoppingList.get(position);
        db.updateStatus(item.getId(), status);
        shoppingList.get(position).setStatus(status);
    }
Related