I have a customized list-view and each row of the list-view contains a check box and a text field. Clicking on any check-box in any row of the list-view should disable other check-boxes.How to achieve this.
-
check here -- http://stackoverflow.com/questions/9844061/android-how-do-i-enable-disable-a-checkbox-depending-on-a-radio-button-being-s – Tasos Jan 23 '16 at 08:13
4 Answers
declare an array list of checkbox. Inside your custom adapter in getView method :
put checkbox inside an arraylist.
if(your condition==true) {
for(CheckBox c: lsitOfCheckBox){
c.setEnabled(false);
}
}

- 3,868
- 10
- 36
- 59

- 2,235
- 16
- 25
-
But if I do this then it ill disable only the check box which I have clicked. I want to disable other check boxes in the list if check box of any row is clicked – Abhishek Jan 23 '16 at 08:22
-
Inside your custom adapter in getView method :
if(your condition==true){
checkBox.setEnabled(false);
} else {
checkBox.setEnabled(true);
}

- 3,868
- 10
- 36
- 59

- 36
- 1
First of all you should use recycler view, it is way better and practical than listview.
To-do that you need to have a custom adapter class which is extended from BaseAdapter.
You need have a List list for your adapter to iterate over. And in your custommodel you need to add new field called private boolean selected'
.
Then you need to define onclick listeners for the checkbox in the getview()
method of the adapter.
Then in the onclicklistener.onclick
method you should get the position of the clicked item. And iterate over all your data and set 'item.selected = false;'
and only set list[clickPosition].selected = true;
Then call notifyDataSetChanged();
I hope it is clear for you, if not post some more data.

- 6,807
- 3
- 43
- 37
First I'd suggest using a RecyclerView, I'll answer the question for a ListView, but really either way the solutions will be similar.
Assuming you're using the ViewHolder pattern in your BaseAdapter. Inside your getView function set a onCheckedChangedListener and inside that create a function that sets an isListChecked
boolean inside the class that implements your adapter's Interface for setListChecked
, should look something like:
cbListItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
myInterface.setListChecked(isChecked);
notifyDatasetChanged();
}
}
);
Inside getView you will also need to check the isListChecked
boolean and set that as the CheckBox state

- 632
- 2
- 11
- 29