Android:ListViews 和 CheckBoxes 的问题 [英] Android: Problem With ListViews and CheckBoxes

查看:20
本文介绍了Android:ListViews 和 CheckBoxes 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView,在每个列表项中我有一些 TextViews 和一个 CheckBox.当我检查 CheckBox 并且我的 onCheckedChangeListener 触发时,一切正常.但是,一旦选中一个复选框,就会选中其他随机复选框.这是一个例子.

I have a ListView, and within each list item I have some TextViews and a CheckBox. When I check a CheckBox and my onCheckedChangeListener fires, everything works as it should. However, random other checkboxes get checked once one is checked. Here is an example.

如果我点击第一个 CheckBox:8 被检查.15 被检查.21 被检查.27 被检查.33 被检查.41 被检查.然后,如果我一直向上滚动,直到 6 点才会检查.下一个是 13 点.

If I click on the first CheckBox: 8 is checked. 15 is checked. 21 is checked. 27 is checked. 33 is checked. 41 is checked. Then if I scroll all the way up, none are checked until 6. The next being 13.

基本上……这是怎么回事?

Basically... what is going on?

推荐答案

看来您正在重用传递给您的 getView() 方法的 convertView实施.

It seems that you are reusing the convertView that is passed on the getView() method that you implement.

Android 将尝试对 ListView 中的不同项目使用相同的视图.您将需要 (1) 手动取消选中/选中返回项内的复选框(在返回 getView 之前始终调用 setChecked 或 (2) 不使用 convertView,但返回一个新视图来自 getView.

Android will try to use the same view for different items in a ListView. You will either need to (1) uncheck/check manually the checkbox that is inside the returned item (always call setChecked before returning on getView or (2) don't use convertView, but return a new View from getView.

(1) 是推荐的,我认为.

(1) is recommended, I think.

这篇关于Android:ListViews 和 CheckBoxes 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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