解决 Android 中使用ListView和CheckBox批量操作时若干问题
解决 Android 中使用ListView和CheckBox批量操作时若干问题
本文可以帮助 完美解决 在Android中使用ListView时批量操作CheckBox出现的各种问题。
在Android中使用ListActivity可以很方便的绑定一组数据或者一个查询。但是,使用过程中也会遇到一些问题。在此,我将自己遇到的问题以及解决方法记录下来,一方面做一个备忘,同时,也希望有缘人能少走弯路。
问题一: Listview中的Item数目到底是多少
ListView中的Item数目可以使用getCount方法获得,经过验证得到的结果是,其Item数目等于界面上显示的Item数目,这个数目可能小于实际上绑定的数据条目数。
那么,在实际中如果有额外的非绑定数据源的数据需要编辑保存的时候,如何才能保存他们呢?
解决该问题的方法是:自定义ListAdapter,在其中保存额外需要保存的数据。
问题二:在Item中添加CheckBox出现麻烦了
在item中添加Checkbox的时候不小心会遇到麻烦,可能出现的情况是:
(1)Listview不能相应点击事件
(2)Listview点击第一个Item的时候最后一个Item也出现点击事件(反之亦然)
以上两种情况是我实际遇到的bug,经过各种纠结和反复测试,出现问题的原因是CheckBox相应焦点、点击事件的优先级别比Listview要高,所以出现问题。
解决方法如下(和问题一一对应):
(1)将Checkbox设置focusable属性为false
(2)接着将CheckBox设置Clickable属性为false.
以下是本人程序片段,仅供参考:
- <!-- ans_list.xml -->
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:paddingLeft="8dp"
- android:paddingRight="8dp" >
- <TextView
- android:id="@+id/ans_title"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:gravity="center"
- android:textSize="15sp"
- android:textStyle="bold" />
- <RelativeLayout
- android:id="@+id/ans_foot"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:paddingLeft="8sp"
- android:paddingRight="8sp" >
- <CheckBox
- android:id="@+id/ans_cbx_select"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_marginLeft="10sp"
- android:checked="false" />
- <Button
- android:id="@+id/ans_btn_showInMap"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_toLeftOf="@id/ans_cbx_select"
- android:text="@string/ans_btn_showInMap" />
- </RelativeLayout>
- <ListView
- android:id="@android:id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_above="@id/ans_foot"
- android:layout_below="@id/ans_title"
- android:drawSelectorOnTop="false" >
- </ListView>
- <TextView
- android:id="@android:id/empty"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:text="@string/ans_list_empty"
- android:textSize="25sp"
- android:textStyle="bold" >
- </TextView>
- </RelativeLayout>
|
评论暂时关闭