自定义列表视图与复选框问题 [英] Custom listview with checkbox problem
问题描述
我正在尝试创建一个自定义列表,其中包含允许您从列表中选择多个项目的复选框。
I'm trying to create a custom list which will have checkboxes that allow you to select several items from the list.
带有复选框的列表显示为ok但是如果我检查一个复选框,然后滚动其他项目进一步向下列表也被检查。
The list with the checkboxes is displayed ok but if i check a checkbox and then scroll other items further down the list are also checked.
它基本上与这里
我知道它和android的方式有关回收视图,但我不能看到如何解决这个问题!有人可以帮助我吗?
I understand that it has something to do with the way android recycles the view but I cant see how to fix this! Can somebody help me???
谢谢 - Mike
Thanks -- Mike
推荐答案
您需要一个数据结构来跟踪哪些行被选中。这可以像 bool [] checked
一样简单。
You need a data structure to keep track of which rows are checked. This could be as simple as a bool[] checked
.
在 getView
,请确保将复选框状态设置为 checked [position]
的内容。您还应在您的网站上设置OnCheckedChangedListener 在 getView
中的复选框,以便他们使用 checked [position] = isChecked
更新您的数据。
In your getView
, make sure that you set the checkbox state to the contents of checked[position]
. You should also set an OnCheckedChangedListener on your check boxes in getView
so that they update your data with checked[position] = isChecked
.
是的, ListView
中的行被回收,因此请确保填充一行的所有适当数据,然后再保留 getView
。
Yes, the rows in a ListView
are recycled, so make sure to populate all the appropriate data for a row before you leave getView
.
这篇关于自定义列表视图与复选框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!