Kiran Maheshwari Kiran Maheshwari - 3 months ago 39
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);
}

@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.

Answer

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

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

Comments