带有复选框问题的自定义列表视图 [英] Custom listview with checkbox problem
问题描述
我正在尝试创建一个自定义列表,其中包含允许您从列表中选择多个项目的复选框.
带有复选框的列表显示正常,但如果我选中一个复选框,然后将其他项目进一步向下滚动,列表也会被选中.
它与此处
我知道这与 android 回收视图的方式有关,但我不知道如何解决这个问题!有人可以帮我吗???
谢谢——迈克
您需要一个数据结构来跟踪检查了哪些行.这可以像bool[]检查
一样简单.
在您的 getView
中,确保将复选框状态设置为 checked[position]
的内容.您还应该设置一个 OnCheckedChangedListener 在 getView
中的复选框上,以便它们使用 checked[position] = isChecked
更新您的数据.
是的,ListView
中的行会被回收,因此请确保在离开 getView
之前填充一行的所有适当数据.
I'm trying to create a custom list which will have checkboxes that allow you to select several items from the list.
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.
Its basically the same problem as here
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???
Thanks -- Mike
You need a data structure to keep track of which rows are checked. This could be as simple as a bool[] checked
.
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
.
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屋!