Kiran Maheshwari Kiran Maheshwari - 2 months ago 22x
Android Question

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);

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);

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

//in some cases, it will prevent unwanted situations

//if true, your checkbox will be selected, else unselected

holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (supportSchoolIdModels.get(position).getPkSchoolId().equalsIgnoreCase(supportSchoolId)) {
} else {
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) {
schoolIdTxt = (TextView) v.findViewById(;
schoolNameTxt = (TextView) v.findViewById(;
checkbox = (CheckBox) v.findViewById(;
public void bindData(SupportSchoolIdModel supportSchoolIdModel) {

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


You can create a variable inside Adapter class that can be used to hold selected position.

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

and inside OnBindViewHolder set checkState

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

So inside setOnCheckedChangeListener you must update position

this.selectedPosition = holder.getAdapterPosition();

So refresh adapter



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){ setChecked(true)} else{ setChecked(false) }