解决 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.

以下是本人程序片段,仅供参考:

  1. <!-- ans_list.xml -->  
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:paddingLeft="8dp"  
  7.     android:paddingRight="8dp" >  
  8.   
  9.     <TextView  
  10.         android:id="@+id/ans_title"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_alignParentTop="true"  
  14.         android:gravity="center"  
  15.         android:textSize="15sp"  
  16.         android:textStyle="bold" />  
  17.   
  18.     <RelativeLayout  
  19.         android:id="@+id/ans_foot"  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignParentBottom="true"  
  23.         android:paddingLeft="8sp"  
  24.         android:paddingRight="8sp" >  
  25.   
  26.         <CheckBox  
  27.             android:id="@+id/ans_cbx_select"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content"  
  30.             android:layout_alignParentRight="true"  
  31.             android:layout_marginLeft="10sp"  
  32.             android:checked="false" />  
  33.         <Button  
  34.             android:id="@+id/ans_btn_showInMap"  
  35.             android:layout_width="fill_parent"  
  36.             android:layout_height="wrap_content"  
  37.             android:layout_toLeftOf="@id/ans_cbx_select"  
  38.             android:text="@string/ans_btn_showInMap" />  
  39.     </RelativeLayout>  
  40.   
  41.     <ListView  
  42.         android:id="@android:id/list"  
  43.         android:layout_width="fill_parent"  
  44.         android:layout_height="fill_parent"  
  45.         android:layout_above="@id/ans_foot"  
  46.         android:layout_below="@id/ans_title"  
  47.         android:drawSelectorOnTop="false" >  
  48.     </ListView>  
  49.   
  50.     <TextView  
  51.         android:id="@android:id/empty"  
  52.         android:layout_width="fill_parent"  
  53.         android:layout_height="fill_parent"  
  54.         android:gravity="center"  
  55.         android:text="@string/ans_list_empty"  
  56.         android:textSize="25sp"  
  57.         android:textStyle="bold" >  
  58.     </TextView>  
  59.   
  60. </RelativeLayout>  
  • 1
  • 2
  • 3
  • 下一页

相关内容