原文网址:http://blog.csdn.net/onlyonecoder/article/details/8687811

Demo地址(0分资源)http://download.csdn.net/detail/onlyonecoder/5154352

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

布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns: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布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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:

  1. public class Ex_checkboxActivity extends Activity {
  2. private ListView lv;
  3. private MyAdapter mAdapter;
  4. private ArrayList<String> list;
  5. private Button bt_selectall;
  6. private Button bt_cancel;
  7. private Button bt_deselectall;
  8. private int checkNum; // 记录选中的条目数量
  9. private TextView tv_show;// 用于显示选中的条目数量
  10. /** Called when the activity is first created. */
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  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 = new ArrayList<String>();
  22. // 为Adapter准备数据
  23. initDate();
  24. // 实例化自定义的MyAdapter
  25. mAdapter = new MyAdapter(list, this);
  26. // 绑定Adapter
  27. lv.setAdapter(mAdapter);
  28. // 全选按钮的回调接口
  29. bt_selectall.setOnClickListener(new OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. // 遍历list的长度,将MyAdapter中的map值全部设为true
  33. for (int i = 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(new OnClickListener() {
  44. @Override
  45. public void onClick(View v) {
  46. // 遍历list的长度,将已选的设为未选,未选的设为已选
  47. for (int i = 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(new OnClickListener() {
  62. @Override
  63. public void onClick(View v) {
  64. // 遍历list的长度,将已选的按钮设为未选
  65. for (int i = 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(new OnItemClickListener() {
  77. @Override
  78. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  79. long arg3) {
  80. // 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
  81. ViewHolder holder = (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. private void initDate() {
  99. for (int i = 0; i < 15; i++) {
  100. list.add("data" + " " + i);
  101. }
  102. ;
  103. }
  104. // 刷新listview和TextView的显示
  105. private void dataChanged() {
  106. // 通知listView刷新
  107. mAdapter.notifyDataSetChanged();
  108. // TextView显示最新的选中数目
  109. tv_show.setText("已选中" + checkNum + "项");
  110. };
  111. }

列表适配器:

  1. package com.notice.listcheck;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import android.content.Context;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.CheckBox;
  10. import android.widget.TextView;
  11. public class MyAdapter extends BaseAdapter {
  12. // 填充数据的list
  13. private ArrayList<String> list;
  14. // 用来控制CheckBox的选中状况
  15. private static HashMap<Integer, Boolean> isSelected;
  16. // 上下文
  17. private Context context;
  18. // 用来导入布局
  19. private LayoutInflater inflater = null;
  20. // 构造器
  21. public MyAdapter(ArrayList<String> list, Context context) {
  22. this.context = context;
  23. this.list = list;
  24. inflater = LayoutInflater.from(context);
  25. isSelected = new HashMap<Integer, Boolean>();
  26. // 初始化数据
  27. initDate();
  28. }
  29. // 初始化isSelected的数据
  30. private void initDate() {
  31. for (int i = 0; i < list.size(); i++) {
  32. getIsSelected().put(i, false);
  33. }
  34. }
  35. @Override
  36. public int getCount() {
  37. return list.size();
  38. }
  39. @Override
  40. public Object getItem(int position) {
  41. return list.get(position);
  42. }
  43. @Override
  44. public long getItemId(int position) {
  45. return position;
  46. }
  47. @Override
  48. public View getView(int position, View convertView, ViewGroup parent) {
  49. ViewHolder holder = null;
  50. if (convertView == null) {
  51. // 获得ViewHolder对象
  52. holder = new ViewHolder();
  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. return convertView;
  68. }
  69. public static HashMap<Integer, Boolean> getIsSelected() {
  70. return isSelected;
  71. }
  72. public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
  73. MyAdapter.isSelected = isSelected;
  74. }
  75. public static class ViewHolder {
  76. TextView tv;
  77. CheckBox cb;
  78. }
  79. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

【转】Android 带checkbox的listView 实现多选,全选,反选 -- 不错的更多相关文章

  1. Android 带checkbox的listView 实现多选,全选,反选,删除

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...

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

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

  3. 【转】Android 带checkbox的listView 实现多选,全选,反选----解决checkbox错位问题

    原文网址:http://blog.csdn.net/onlyonecoder/article/details/8687811 Demo地址(0分资源):http://download.csdn.net ...

  4. 带CheckBox美化控件的表格全选

    带CheckBox美化控件 <table class="positionTable commonListTable" id="positionTable" ...

  5. 【转】带checkbox的ListView实现(二)——自定义Checkable控件的实现方法

    原文网址:http://blog.csdn.net/harvic880925/article/details/40475367 前言:前一篇文章给大家展示了传统的Listview的写法,但有的时候我们 ...

  6. Android ListView 中加入CheckBox/RadioButton 选择状态保持、全选、反选实现

    最近在一个项目中,需要在ListView的item中加入CheckBox,但是遇到的一个问题是上下滑动的时候如果有选择了的CheckBox,就会出现选择项错误的问题,下面将个人的解决方法总结如下;先说 ...

  7. Android ListView批量选择(全选、反选、全不选)

    APP的开发中,会常遇到这样的需求:批量取消(删除)List中的数据.这就要求ListVIew支持批量选择.全选.单选等等功能,做一个比较强大的ListView批量选择功能是很有必要的,那如何做呢? ...

  8. 在Indicator中添加动态Checkbox,无需绑定数据源,支持全选 - Ehlib学习(二)

    先做设置 DBGrideh属性设置: IndicatorOptions = [gioShowRowIndicatorEh, //小三角指示 gioShowRecNoEh, //数据源行号 gioSho ...

  9. wpf中为DataGrid添加checkbox支持多选全选

    项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选.全选. 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellContro ...

随机推荐

  1. [转] Java中的容器

    在书写程序的时候,我们常常需要对大量的对象引用进行管理.为了实现有效的归类管理,我们常常将同类的引用放置在同一数据容器中. 由于数据容器中存放了我们随时可能需要使用到的对象引用,所以一般的数据容器要都 ...

  2. jquery ajax 提交表单(file && input)

    用到的插件 jquery.js jquery.form.js[http://malsup.github.io/jquery.form.js] 提交页面 <form enctype="m ...

  3. 国都企信通短信平台发送手机短信的python脚本一例

    一年前,由于工作需要,给以色列的同事解释一下国都短信平台的短信发送格式,本来不懂python的我硬着头皮写了一个sample,比较粗,能用,但不优美,希望以后学会python能改得像我同事写的那么优雅 ...

  4. TwoSAT算法模板

    该模板来自大白书 [解释] 给多个语句,每个语句为“ Xi为真(假) 或者 Xj为真(假)” 每个变量和拆成两个点 2*i为假, 2*i+1为真 “Xi为真 或 Xj为真”  等价于 “Xi为假 –& ...

  5. ubuntu 配置Java jdk

    本文参考:http://www.cnblogs.com/memory4young/p/ubuntu-install-jdk.html 一.下载 到oracle官方网站下载jdk,博主下载时的版本是8u ...

  6. AlertDialog dismiss 和 cancel方法的区别

    AlertDialog使用很方便,但是有一个问题就是:dismiss方法和cancel方法到底有什么不同? AlertDialog继承与Dialog,现在各位看看结构图: 然后在Dialog类中找到了 ...

  7. 后台地址报错:Service Unavailable

    首先考虑数据库是否打开? 第二重启IIS试试: 重启下iis试试(cmd接着iisreset) 再次访问就正常了,可以借鉴,但不一定就只是这一种原因.

  8. JS加入收藏与设置主页

    收藏: <a href="javascript:void(0)" onclick="shoucang(document.title,window.location) ...

  9. 利用Range改变光标位置

    先上代码,代码取自网上某插件中 function caret(begin, end) { if (this.length == 0) return; if (typeof begin == 'numb ...

  10. 使用<br>标签分行显示文本

    对于上一小节的例子,我们想让那首诗显示得更美观些,如显示下面效果: 怎么可以让每一句诗词后面加入一个折行呢?那就可以用到<br />标签了,在需要加回车换行的地方加入<br /> ...