Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select only one checkbox in Recyclerview and notifydataset changed

In my code I have create recyclerview with check box and default one item selected already. now I want when select other item checkbox so deselect all other items mean one item select at time.

My Adapter code:

public class SupportSchoolIdAdapter extends RecyclerView.Adapter<SupportSchoolIdAdapter.ViewHolder> {

ArrayList<SupportSchoolIdModel> supportSchoolIdModels;
DataPref mDataPref;
String supportSchoolId;

public SupportSchoolIdAdapter(List<SupportSchoolIdModel> supportSchoolIdModels) {
    this.supportSchoolIdModels = new ArrayList<>(supportSchoolIdModels);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    mDataPref = DataPref.getInstance(context);
    supportSchoolId = mDataPref.getSupportSchoolId();
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.support_school_item, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    final SupportSchoolIdModel event = supportSchoolIdModels.get(position);
    holder.bindData(supportSchoolIdModels.get(position));

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

    //if true, your checkbox will be selected, else unselected
    holder.checkbox.setChecked(supportSchoolIdModels.get(position).isSelected());

    holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            supportSchoolIdModels.get(holder.getAdapterPosition()).setSelected(isChecked);
        }
    });
    if (supportSchoolIdModels.get(position).getPkSchoolId().equalsIgnoreCase(supportSchoolId)) {
        holder.checkbox.setChecked(true);
    } else {
        holder.checkbox.setChecked(false);
    }
}
@Override
public int getItemCount() {
    return supportSchoolIdModels.size();
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    private TextView schoolIdTxt;
    private TextView schoolNameTxt;
    private CheckBox checkbox;

    public ViewHolder(View v) {
        super(v);
        schoolIdTxt = (TextView) v.findViewById(R.id.schoolIdTxt);
        schoolNameTxt = (TextView) v.findViewById(R.id.schoolNameTxt);
        checkbox = (CheckBox) v.findViewById(R.id.checkbox);
    }
    public void bindData(SupportSchoolIdModel supportSchoolIdModel) {
        schoolIdTxt.setText(supportSchoolIdModel.getPkSchoolId());
        schoolNameTxt.setText(supportSchoolIdModel.getSchoolName());
    }
}
}

And Second problem is when I scroll recycle view why selected item unchecked. please help me.

like image 749
Kiran Maheshwari Avatar asked Aug 24 '16 15:08

Kiran Maheshwari


People also ask

How do I select a single item in RecyclerView?

For single selection, we just need to hold the position that clicked in the recyclerview. When user clicks the another position, forget previous one and make new position as hold position.

How do you update one item in RecyclerView?

Update single item Change the "Sheep" item so that it says "I like sheep." String newValue = "I like sheep."; int updateIndex = 3; data. set(updateIndex, newValue); adapter. notifyItemChanged(updateIndex);

Why is RecyclerView used select one?

Thanks to this, RecyclerView doesn't have to think about how to position the row view. This class gives us the opportunity to choose the way that we want to show the row views and how to scroll the list. For example, if we want to scroll our list vertically or horizontally, we can choose LinearLayoutManager.

What is the difference between onCreateViewHolder and onBindViewHolder?

This method internally calls onBindViewHolder to update the ViewHolder contents with the item at the given position and also sets up some private fields to be used by RecyclerView. This method calls onCreateViewHolder to create a new ViewHolder and initializes some private fields to be used by RecyclerView.


1 Answers

1- You can create a variable inside Adapter class that can be used to hold the selected position:

private int selectedPosition = -1;// no selection by default

2- Inside onBindViewHolder set Check State:

holder.checkBox.setChecked(selectedPosition == position);

3- Inside setOnCheckedChangeListener you must update position

this.selectedPosition = holder.getAdapterPosition();

4- Refresh adapter

adapter.notifyDatasetChanged();

EDIT

onBindViewHolder will be called for all positions,

So method setChecked will called for all CheckBoxes, This method have a boolean input.

In above example i write setChecked(selectedPosition == position). For more readable i can write:

if(selectedPosition == position){ 
   holder.checkBox.setChecked(true);
}
else{
   holder.checkBox.setChecked(false);
}
like image 55
an_droid_dev Avatar answered Oct 12 '22 23:10

an_droid_dev