gpt4 book ai didi

android - ListView 中的复选框 - 当我选择第一个时,它会检查最后一个,反之亦然

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:03:30 26 4
gpt4 key购买 nike

我设置了一个 ListView,它在每一行中都包含一个 Checkbox(以及一个 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.

我知道这里没有任何代码可以使用,但我只是想知道这是否是一个普遍问题?如果没有,我可以尝试发布一些代码。谢谢!

最佳答案

View 在 ListView 中被回收。这就是为什么有些人会在您认为不应该检查的时候进行检查。

事情是这样的:复选框不知道它代表适配器中的哪个项目。它只是 ListView 中一行中的一个复选框。您需要做一些事情来“教导”行,它们当前显示的是数据集中的哪个项目。因此,不要使用像字符串数组这样简单的东西作为适配器的数据,而是创建一个新的模型对象来存储复选框的状态。然后,在返回 getView() 中的行之前,您可以执行如下操作:

//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.

关于android - ListView 中的复选框 - 当我选择第一个时,它会检查最后一个,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8511433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com