Android 取消选中 ListView 上的复选框
我有一个带有自定义适配器的 ListView
。它包含复选框。当其中之一被检查时,我希望不检查其他的。我尝试创建一个 CheckBox
列表,然后创建一个方法来返回此列表,但它不断给出 IndexOutOfBounds (我将 CheckBoxes 添加到 getView 方法的列表中)。
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox android:id="@+id/checkBoxListView"
android:layout_height="50px"
android:layout_width="50px"/>
<EditText android:id="@+id/editTxtListView"
android:layout_height="50px"
android:layout_width="680px"
android:editable="false"
android:focusable="false" />
I have a ListView
with a custom adapter. It countains CheckBoxes. When one of them is checked, I would like to have the others unchecked. I tried creating a CheckBox
list, and then created a method to return this list, but it kept giving IndexOutOfBounds (I added the CheckBoxes to the list at the getView method).
The xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox android:id="@+id/checkBoxListView"
android:layout_height="50px"
android:layout_width="50px"/>
<EditText android:id="@+id/editTxtListView"
android:layout_height="50px"
android:layout_width="680px"
android:editable="false"
android:focusable="false" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么没有一个全局整数来表示选中的位置,并在 getview 方法中查看全局变量的值和调用 getView 的位置是否相同,然后选中复选框,否则不选中。
看到你的代码
你让它变得相当复杂。
无论如何,只是一个简短的评论,
不要这样做this.pos=position;
getView().
而不是选中此复选框 if(position==this.pos){
//选中此复选框}
else{
//取消选中此复选框
}
此外,“pos”应该由处理列表数据的活动或 pojo 本身控制。
why not have a global integer representing the checked position and in the getview method see if the global variable's value and the position, for which getView is called, are same then make check box checked else unchecked.
Saw your code in the pastebin.
You are making it quite a complex thing.
Anyway, just a quick comment,
Dont do this.pos=position;
in the getView().
Instead of this check if(postion==this.pos){
//Check this checkbox}
else{
//uncheck this check box
}
Also, "pos" should be controlled by the activity or the pojo itself handling the data for the list.