Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom listview with checkbox problem

I'm trying to create a custom list which will have checkboxes that allow you to select several items from the list.

The list with the checkboxes is displayed ok but if i check a checkbox and then scroll other items further down the list are also checked.

Its basically the same problem as here

I understand that it has something to do with the way android recycles the view but I cant see how to fix this! Can somebody help me???

Thanks -- Mike

like image 304
mixkat Avatar asked Mar 25 '11 21:03

mixkat


2 Answers

You need a data structure to keep track of which rows are checked. This could be as simple as a bool[] checked.

In your getView, make sure that you set the checkbox state to the contents of checked[position]. You should also set an OnCheckedChangedListener on your check boxes in getView so that they update your data with checked[position] = isChecked.

Yes, the rows in a ListView are recycled, so make sure to populate all the appropriate data for a row before you leave getView.

like image 58
Matthew Willis Avatar answered Oct 27 '22 21:10

Matthew Willis


You could try implementing OnClickListener for checkbox instead of OnCheckChangedListener. It worked for me.

like image 35
iluu Avatar answered Oct 27 '22 20:10

iluu