0

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.

Adarsh Yadav
  • 3,752
  • 3
  • 24
  • 46
Abhishek
  • 650
  • 1
  • 8
  • 31
  • 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 Answers4

0

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

 } 
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
0

Inside your custom adapter in getView method :

if(your condition==true){
      checkBox.setEnabled(false);
} else {
      checkBox.setEnabled(true);
}
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
0

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.

Gunhan
  • 6,807
  • 3
  • 43
  • 37
0

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

Derek
  • 632
  • 2
  • 11
  • 29