在ListView复选框 - 当我选择的第一个,它会检查最后,反之亦然 [英] Checkboxes in a ListView - When I select the first, it checks the last and vice versa

查看:103
本文介绍了在ListView复选框 - 当我选择的第一个,它会检查最后,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个包含各行中的复选框一个ListView(连同一个TextView)。出于某种原因,当我检查的一个盒子,它似乎检查取其框相反在ListView(即选择时,箱顶,底部的复选框成为选中而前保持未选中状态。

I've set up a ListView that contains a Checkbox in each row (along with a TextView). For some reason, when I "check" one of the boxes, it seems to "check" whichever box is opposite in the ListView (i.e. when selecting the top box, the bottom checkbox becomes selected while the top remains unchecked.

我知道没有任何code这里工作,但我只是想知道,如果这是一个普遍的问题?如果没有,我可以尝试张贴一些code。谢谢!

I realize there isn't any code here to work with, but I was just wondering if this is a general problem? If not, I can try to post some code. Thanks!

推荐答案

意见被回收在ListView。这就是为什么当你觉得不应该有的检查。

Views are recycled in a ListView. That's why some are checked when you think shouldn't be.

这里的交易:该复选框有不知道它重新哪些项目在您的适配器presents。这是一排在ListView只是一个复选框。你需要做一些事来教的项目在你的数据集,他们正在显示的行。因此,而不是用一些简单的一个String数组,为您的适配器中的数据,创建一个存储在其中的复选框的状态一个新的模型对象。然后,返回的行前 getView(),你可以这样做:

Here's the deal: The checkbox has no idea which item in your adapter it represents. It's just a checkbox in a row in a ListView. You need to do something to "teach" the rows which item in your data set they are currently displaying. So, instead of using something as simple as a String array as the data for your adapter, create a new model object that stores the state of the checkbox in it. Then, before you return the row in getView(), you can do something like:

//somewhere in your class
private RowData getData(int position) {
return(((MyAdapter)getListAdapter()).getItem(position)); 
}



 //..then in your adapter in getView()

    RowData object = getModel(position);

     if(object.isChecked()) {
       myCheckbox.setChecked(true);
     } else {
       myCheckbox.setChecked(false);
      }

     //then retun your view.

这篇关于在ListView复选框 - 当我选择的第一个,它会检查最后,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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