Android ListView with Checkbox:自动取消选中 [英] Android ListView with Checkbox: automatically unchecks

查看:35
本文介绍了Android ListView with Checkbox:自动取消选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义 BaseAdapter 的 ListView.列表项包含需要代表数据库属性的复选框.

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 来检测更改,因此我可以根据 CheckBox 的当前状态更改数据库.到目前为止非常简单的东西.

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.

然而,当进一步向下滚动列表时,先前选中的复选框将被取消选中.我怀疑这会在视图被回收后立即发生(我使用的是 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 == false.

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(null) 成功了.

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 with Checkbox:自动取消选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆