Android的ListView控件与复选框:自动取消选中 [英] Android ListView with Checkbox: automatically unchecks
问题描述
我有一个自定义的BaseAdapter一个ListView。列表项包含需要从数据库中重新present属性复选框。
I've got a ListView with a custom BaseAdapter. The list items contain CheckBoxes that need to represent a property from a database.
我用CheckBox.setOnCheckedChangeListener一个新的OnCheckedChangeListener及时发现变化,这样我就可以根据复选框的当前状态更改数据库。 pretty的简单的东西为止。
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.
然而,进一步在列表中向下滚动时,previously选中的复选框取消选中获得。我猜想一旦意见被循环发生这种情况(我使用了convertView / ViewHolder技术)。
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?
在此先感谢。
编辑:为了让事情更清楚一点,问题是,回收的观点在某种程度上要求OnCheckedChangeListener#onCheckedChanged(buttonView,器isChecked)与==器isChecked假
To make things a bit clearer, the problem is that recycling views somehow calls OnCheckedChangeListener#onCheckedChanged(buttonView, isChecked) with isChecked == false.
推荐答案
显然的问题是,通过获取使用convertView.findViewById()复选框,则onCheckedChangeListeners仍然完好无缺,如果有人回收。调用checkbox.setOnCheckedChangeListener(空),并获得成功。
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.
这篇关于Android的ListView控件与复选框:自动取消选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!