自定义列表视图中使用复选框的问题 [英] 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.
将显示该复选框名单确定,但如果我检查复选框,然后再滚动等物品倒也检查清单。
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.
及其基本相同的问题,因为<一个href="http://groups.google.com/group/android-developers/browse_thread/thread/f46c24e955d1f6e2">here
Its basically the same problem as here
据我所知,它是与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???
感谢迈克 -
推荐答案
您需要一个数据结构来跟踪哪些行进行检查。这可能是简单的一个布尔[]检查
。
You need a data structure to keep track of which rows are checked. This could be as simple as a bool[] checked
.
在你的 getView
,确保您设置的复选框状态的内容检查[位置]
。你也应该<一个href="http://developer.android.com/reference/android/widget/CompoundButton.html#setOnCheckedChangeListener%28android.widget.CompoundButton.OnCheckedChangeListener%29"相对=nofollow>在 getView
您的复选框,设置OnCheckedChangedListener ,使他们与选中[位置] =器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屋!