摘抄至:http://www.cnblogs.com/tianshidechibang234/p/3441106.html

Android 带checkbox的listView 实现多选,全选,反选

由于listview的一些特性,刚开始写这种需求的功能的时候都会碰到一些问题,重点就是存储每个checkbox的状态值,在这里分享出了完美解决方法




布局文件:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="horizontal">
  6. <TextView
  7. android:id="@+id/tv"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_vertical"/>
  11. <LinearLayout
  12. android:id="@+id/line"
  13. android:layout_width="fill_parent"
  14. android:layout_height="50dp"
  15. android:layout_below="@+id/tv"
  16. android:orientation="horizontal">
  17. <Button
  18. android:id="@+id/bt_selectall"
  19. android:layout_width="80dp"
  20. android:layout_height="fill_parent"
  21. android:text="全选"/>
  22. <Button
  23. android:id="@+id/bt_cancleselectall"
  24. android:layout_width="80dp"
  25. android:layout_height="fill_parent"
  26. android:text="反选"/>
  27. <Button
  28. android:id="@+id/bt_deselectall"
  29. android:layout_width="80dp"
  30. android:layout_height="fill_parent"
  31. android:text="取消选择"/>
  32. </LinearLayout>
  33. <ListView
  34. android:id="@+id/lv"
  35. android:layout_width="fill_parent"
  36. android:layout_height="fill_parent"
  37. android:layout_below="@+id/line"/>
  38. </RelativeLayout>


listView 的item布局文件:


[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="horizontal">
  6. <TextView
  7. android:id="@+id/item_tv"
  8. android:layout_width="0dp"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_vertical"
  11. android:layout_weight="1"/>
  12. <CheckBox
  13. android:id="@+id/item_cb"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:clickable="false"
  17. android:focusable="false"
  18. android:focusableInTouchMode="false"
  19. android:gravity="center_vertical"/>
  20. </LinearLayout>


Activity:


[java] view plain copy
  1. publicclassEx_checkboxActivityextendsActivity{
  2. privateListViewlv;
  3. privateMyAdaptermAdapter;
  4. privateArrayList<String>list;
  5. privateButtonbt_selectall;
  6. privateButtonbt_cancel;
  7. privateButtonbt_deselectall;
  8. privateintcheckNum;//记录选中的条目数量
  9. privateTextViewtv_show;//用于显示选中的条目数量
  10. /**Calledwhentheactivityisfirstcreated.*/
  11. @Override
  12. publicvoidonCreate(BundlesavedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. /*实例化各个控件*/
  16. lv=(ListView)findViewById(R.id.lv);
  17. bt_selectall=(Button)findViewById(R.id.bt_selectall);
  18. bt_cancel=(Button)findViewById(R.id.bt_cancelselectall);
  19. bt_deselectall=(Button)findViewById(R.id.bt_deselectall);
  20. tv_show=(TextView)findViewById(R.id.tv);
  21. list=newArrayList<String>();
  22. //为Adapter准备数据
  23. initDate();
  24. //实例化自定义的MyAdapter
  25. mAdapter=newMyAdapter(list,this);
  26. //绑定Adapter
  27. lv.setAdapter(mAdapter);
  28. //全选按钮的回调接口
  29. bt_selectall.setOnClickListener(newOnClickListener(){
  30. @Override
  31. publicvoidonClick(Viewv){
  32. //遍历list的长度,将MyAdapter中的map值全部设为true
  33. for(inti=0;i<list.size();i++){
  34. MyAdapter.getIsSelected().put(i,true);
  35. }
  36. //数量设为list的长度
  37. checkNum=list.size();
  38. //刷新listview和TextView的显示
  39. dataChanged();
  40. }
  41. });
  42. //反选按钮的回调接口
  43. bt_cancel.setOnClickListener(newOnClickListener(){
  44. @Override
  45. publicvoidonClick(Viewv){
  46. //遍历list的长度,将已选的设为未选,未选的设为已选
  47. for(inti=0;i<list.size();i++){
  48. if(MyAdapter.getIsSelected().get(i)){
  49. MyAdapter.getIsSelected().put(i,false);
  50. checkNum--;
  51. }else{
  52. MyAdapter.getIsSelected().put(i,true);
  53. checkNum++;
  54. }
  55. }
  56. //刷新listview和TextView的显示
  57. dataChanged();
  58. }
  59. });
  60. //取消按钮的回调接口
  61. bt_deselectall.setOnClickListener(newOnClickListener(){
  62. @Override
  63. publicvoidonClick(Viewv){
  64. //遍历list的长度,将已选的按钮设为未选
  65. for(inti=0;i<list.size();i++){
  66. if(MyAdapter.getIsSelected().get(i)){
  67. MyAdapter.getIsSelected().put(i,false);
  68. checkNum--;//数量减1
  69. }
  70. }
  71. //刷新listview和TextView的显示
  72. dataChanged();
  73. }
  74. });
  75. //绑定listView的监听器
  76. lv.setOnItemClickListener(newOnItemClickListener(){
  77. @Override
  78. publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,
  79. longarg3){
  80. //取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
  81. ViewHolderholder=(ViewHolder)arg1.getTag();
  82. //改变CheckBox的状态
  83. holder.cb.toggle();
  84. //将CheckBox的选中状况记录下来
  85. MyAdapter.getIsSelected().put(arg2,holder.cb.isChecked());
  86. //调整选定条目
  87. if(holder.cb.isChecked()==true){
  88. checkNum++;
  89. }else{
  90. checkNum--;
  91. }
  92. //用TextView显示
  93. tv_show.setText("已选中"+checkNum+"项");
  94. }
  95. });
  96. }
  97. //初始化数据
  98. privatevoidinitDate(){
  99. for(inti=0;i<15;i++){
  100. list.add("data"+""+i);
  101. }
  102. ;
  103. }
  104. //刷新listview和TextView的显示
  105. privatevoiddataChanged(){
  106. //通知listView刷新
  107. mAdapter.notifyDataSetChanged();
  108. //TextView显示最新的选中数目
  109. tv_show.setText("已选中"+checkNum+"项");
  110. };
  111. }


列表适配器:


[java] view plain copy
    1. packagecom.notice.listcheck;
    2. importjava.util.ArrayList;
    3. importjava.util.HashMap;
    4. importandroid.content.Context;
    5. importandroid.view.LayoutInflater;
    6. importandroid.view.View;
    7. importandroid.view.ViewGroup;
    8. importandroid.widget.BaseAdapter;
    9. importandroid.widget.CheckBox;
    10. importandroid.widget.TextView;
    11. publicclassMyAdapterextendsBaseAdapter{
    12. //填充数据的list
    13. privateArrayList<String>list;
    14. //用来控制CheckBox的选中状况
    15. privatestaticHashMap<Integer,Boolean>isSelected;
    16. //上下文
    17. privateContextcontext;
    18. //用来导入布局
    19. privateLayoutInflaterinflater=null;
    20. //构造器
    21. publicMyAdapter(ArrayList<String>list,Contextcontext){
    22. this.context=context;
    23. this.list=list;
    24. inflater=LayoutInflater.from(context);
    25. isSelected=newHashMap<Integer,Boolean>();
    26. //初始化数据
    27. initDate();
    28. }
    29. //初始化isSelected的数据
    30. privatevoidinitDate(){
    31. for(inti=0;i<list.size();i++){
    32. getIsSelected().put(i,false);
    33. }
    34. }
    35. @Override
    36. publicintgetCount(){
    37. returnlist.size();
    38. }
    39. @Override
    40. publicObjectgetItem(intposition){
    41. returnlist.get(position);
    42. }
    43. @Override
    44. publiclonggetItemId(intposition){
    45. returnposition;
    46. }
    47. @Override
    48. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
    49. ViewHolderholder=null;
    50. if(convertView==null){
    51. //获得ViewHolder对象
    52. holder=newViewHolder();
    53. //导入布局并赋值给convertview
    54. convertView=inflater.inflate(R.layout.listviewitem,null);
    55. holder.tv=(TextView)convertView.findViewById(R.id.item_tv);
    56. holder.cb=(CheckBox)convertView.findViewById(R.id.item_cb);
    57. //为view设置标签
    58. convertView.setTag(holder);
    59. }else{
    60. //取出holder
    61. holder=(ViewHolder)convertView.getTag();
    62. }
    63. //设置list中TextView的显示
    64. holder.tv.setText(list.get(position));
    65. //根据isSelected来设置checkbox的选中状况
    66. holder.cb.setChecked(getIsSelected().get(position));
    67. returnconvertView;
    68. }
    69. publicstaticHashMap<Integer,Boolean>getIsSelected(){
    70. returnisSelected;
    71. }
    72. publicstaticvoidsetIsSelected(HashMap<Integer,Boolean>isSelected){
    73. MyAdapter.isSelected=isSelected;
    74. }
    75. publicstaticclassViewHolder{
    76. TextViewtv;
    77. CheckBoxcb;
    78. }
    79. }

更多相关文章

  1. android 反编译心得
  2. android 获取网络数据,回传到本地用TextView显示乱码问题解决方法
  3. Android(安卓)ListView动画(逐行显示动画效果)
  4. 使用Html在EditText中任意位置插入图片并正确显示
  5. LinearLayout不能显示全部内容
  6. android编程常见问题-程序在模拟器中不显示
  7. Android(安卓)之Toast
  8. Android(安卓)Studio 中Copyright 设置
  9. [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的

随机推荐

  1. Android Activity onConfigurationChange
  2. Android showDialog时报错requestFeature
  3. Android01之LinearLayout和RelativeLayou
  4. android实现卸载提示
  5. Android(安卓)Studio Note
  6. Android 分类法:六个类型,八种用户
  7. 浅析Android线程模型一 --- 转
  8. Android 疯狂足球游戏源码
  9. ProgressBar的样式及用法
  10. android删除sd卡文件