简体   繁体   中英

Checkbox clicking check other checkboxes too

When I click my first second and third checkboxes, it also checks last 3 checkboxes?

this is my adapter:

@Override
public void onBindViewHolder(@NonNull final NewGamePlayerViewHolder holder, int position) {
    final NewGamePlayerItem currentItem = mNewGamePlayerList.get(position);

    //in some cases, it will prevent unwanted situations
    holder.mCheckBox.setOnCheckedChangeListener(null);

    //if true, your checkbox will be selected, else unselected
    holder.mCheckBox.setChecked(currentItem.isSelected());

    holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //set your object's last status
            currentItem.setSelected(isChecked);
        }
    });

    holder.mName.setText(currentItem.getmText());
}

This is the item:

package com.example.frisbeecaddy;

public class NewGamePlayerItem {
    private boolean mCheckBox;
    private String mText;

    public NewGamePlayerItem(boolean checkBox, String text) {
        mCheckBox = checkBox;
        mText = text;
    }

    public boolean getCheckBox() {
        return mCheckBox;
    }

    public String getmText() {
        return mText;
    }
}

This is copied from here: CheckBox in RecyclerView keeps on checking different items

but for me isSelected() and setSelected() it says: cannot resolve method...

First, you need to remember what the core meaning of RecyclerView, this sum it all (see RecyclerView glossary of terms ):

Recycle (view): A view previously used to display data for a specific adapter position may be placed in a cache for later reuse to display the same type of data again later. This can drastically improve performance by skipping initial layout inflation or construction.

So, your First problem of:

When I click my first second and third checkboxes, it also checks last 3 checkboxes

means that your same RecyclerView item is being reused in another item. To tackle the problem, you need to add mechanism to hold the check state for each item. You could do it either by using a SparseBooleanArray or modifying your object to have a state variable.

The Second problem:

but for me isSelected() and setSelected() it says: cannot resolve method...

is because of the following code:

final NewGamePlayerItem currentItem = mNewGamePlayerList.get(position);

...

//set your object's last status
currentItem.setSelected(isChecked);

where you're trying to call a non-existed method in your NewGamePlayerItem.

You need to modify your object to something like this:

package com.example.frisbeecaddy;

public class NewGamePlayerItem {
    private boolean mCheckBox;
    private String mText;
    private boolean mIsSelected;

    public NewGamePlayerItem(boolean checkBox, String text, boolean isSelected) {
        mCheckBox = checkBox;
        mText = text;
        mIsSelected = isSelected;
    }

    public boolean getCheckBox() {
        return mCheckBox;
    }

    public String getmText() {
        return mText;
    }


    // the added methods here
    public boolean isSelected() {
       return mIsSelected;
    }

    public void setSelected(boolean isSelected) {
        mIsSelected = isSelected;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM