views:

372

answers:

2

Hi,

I've got a ListView with a custom BaseAdapter. The list items contain CheckBoxes that need to represent a property from a database.

I use CheckBox.setOnCheckedChangeListener with a new OnCheckedChangeListener to detect changes, so I can change the database based on the current state of the CheckBox. Pretty straightforward stuff so far.

However, when scrolling further down the list, previously checked CheckBoxes get unchecked. I suspect this happens as soon as the views are recycled (I'm using the convertView/ViewHolder technique).

How can I stop this? What's going wrong?

Thanks in advance.


Edit: To make things a bit clearer, the problem is that recycling views somehow calls OnCheckedChangeListener#onCheckedChanged(buttonView, isChecked) with isChecked == false.

+1  A: 

Apparently the problem was that, by getting the checkbox using convertView.findViewById(), the onCheckedChangeListeners were still intact if the view was recycled. Calling checkbox.setOnCheckedChangeListener(null) did the trick.

benvd
I was having the same problem when using checkbox in the child view of an ExpandableListView, and your trick worked for me as well. I had to put this in getChildView(..), where I check if we are recycling (i.e., convertView is not null), and set the OnCheckedChangeListener of the checkbox to null in that case. Thanks for the solution.
Samik R.
A: 

I have the same issue. Can someone please explain where I have to set the changeListener to null?

Thx

Bernd