Android中ListView如何获取列表中选中的CheckBox
2014-08-26 12:38
489 查看
如题,在一个ListView中设置了CheckBox,如果选中其中的CheckBox,该如何获取。
![](http://img.bimg.126.net/photo/lotOz1Gm1qjhFsz__rBrnw==/5771644397453033013.jpg)
ListView lstres = (ListView)findViewById(R.id.lstres);// 结果列表
for (int i = 0; i < lstres.getChildCount(); i++) {
LinearLayout ll = (LinearLayout)lstres.getChildAt(i);// 获得子级
CheckBox chkone = (CheckBox) ll.findViewById(R.id.chkone);// 从子级中获得控件
System.out.println("chkone.isChecked():----------->"+chkone.isChecked());
}
如果直接将getChildAt获得的结果转型为CheckBox 会报错,因为ListView的直接子级是LinearLayout ,可以参照xml文件便得知,
list.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="fill_parent">
<TextView android:text="bb" android:id="@+id/txtid"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="200px" android:layout_height="wrap_content">
<TextView android:text="country" android:id="@+id/txtone"
android:textSize="22px" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:text="code" android:id="@+id/txtcode"
android:textSize="13px" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<CheckBox android:id="@+id/chkone" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginRight="0px"/>
</LinearLayout>
![](http://img.bimg.126.net/photo/lotOz1Gm1qjhFsz__rBrnw==/5771644397453033013.jpg)
ListView lstres = (ListView)findViewById(R.id.lstres);// 结果列表
for (int i = 0; i < lstres.getChildCount(); i++) {
LinearLayout ll = (LinearLayout)lstres.getChildAt(i);// 获得子级
CheckBox chkone = (CheckBox) ll.findViewById(R.id.chkone);// 从子级中获得控件
System.out.println("chkone.isChecked():----------->"+chkone.isChecked());
}
如果直接将getChildAt获得的结果转型为CheckBox 会报错,因为ListView的直接子级是LinearLayout ,可以参照xml文件便得知,
list.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="fill_parent">
<TextView android:text="bb" android:id="@+id/txtid"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="200px" android:layout_height="wrap_content">
<TextView android:text="country" android:id="@+id/txtone"
android:textSize="22px" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:text="code" android:id="@+id/txtcode"
android:textSize="13px" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<CheckBox android:id="@+id/chkone" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginRight="0px"/>
</LinearLayout>
相关文章推荐
- 如何获取listview里选中的checkbox
- Android ListView或GridView中含有CheckBox时,获取选中的id
- 如何在Android的ListView中构建CheckBox和RadioButton列表(支持单选和多选的投票项目示例)
- 如何获取datagridview中checkbox选中的多行数据
- 继续聊WPF——如何获取ListView中选中的项
- Android开发_Android如何获取开机启动项列表_查找package是否有开机启动权限
- 如何使listview中的CheckBox被正确选中?
- android如何实现ListView中的CheckBox的全选、反选、全不选
- Android中ListView结合CheckBox判断选中项
- vc CListCtrl 如何判断列表中的内容是否被选中?如何获取CListCtrl当前选中的行
- Android中ListView结合CheckBox判断选中项
- 转载:jquery如何来获取label标签里的内容 checkbox选中后获取label标签内容
- Android开发之ListView,加入CheckBox(复选框),实现选择列表
- Android中ListView结合CheckBox判断选中项
- 【Android特效一】listview固定选中一项,整个列表上移下移。
- Android如何获取文件列表
- 继续聊WPF——如何获取ListView中选中的项
- Android中ListView结合CheckBox判断选中项
- android-如何在ListView中显示单选按钮并能默认选中
- android 获取listview checkbox选择的值