Skip to content Skip to sidebar Skip to footer

Checkboxes In A Listview - When I Select The First, It Checks The Last And Vice Versa

I've set up a ListView that contains a Checkbox in each row (along with a TextView). For some reason, when I 'check' one of the boxes, it seems to 'check' whichever box is opposit

Solution 1:

Views are recycled in a ListView. That's why some are checked when you think shouldn't be.

Here's the deal: The checkbox has no idea which item in your adapter it represents. It's just a checkbox in a row in a ListView. You need to do something to "teach" the rows which item in your data set they are currently displaying. So, instead of using something as simple as a String array as the data for your adapter, create a new model object that stores the state of the checkbox in it. Then, before you return the row in getView(), you can do something like:

//somewhere in your classprivate RowData getData(int position) {
return(((MyAdapter)getListAdapter()).getItem(position)); 
}



 //..then in your adapter in getView()

    RowData object = getModel(position);

     if(object.isChecked()) {
       myCheckbox.setChecked(true);
     } else {
       myCheckbox.setChecked(false);
      }

     //then retun your view.

Post a Comment for "Checkboxes In A Listview - When I Select The First, It Checks The Last And Vice Versa"