本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作。通过一个Demo来展示该功能,选中ListView中的某一项,然后点击Button按钮来显示选中了哪些项。

[1] 程序结构图如下:

listitem.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="horizontal"
  11. android:descendantFocusability="blocksDescendants">
  12. <CheckBox
  13. android:id="@+id/list.select"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"/>
  16. <TextView
  17. android:id="@+id/list.name"
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="1"
  21. android:text="Name"
  22. android:layout_gravity="center"
  23. android:textSize="20dp"
  24. android:layout_marginLeft="10dp"/>
  25. <TextView
  26. android:id="@+id/list.address"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:layout_weight="1"
  30. android:text="Address"
  31. android:layout_gravity="center"
  32. android:textSize="20dp"/>
  33. </LinearLayout>
  34. </LinearLayout>

main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <Button
  7. android:id="@+id/show"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="Show"/>
  11. <ListView
  12. android:id="@+id/lvperson"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"/>
  15. </LinearLayout>

Person.java:

  1. package com.andyidea.bean;
  2.  
  3. public class Person {
  4.  
  5. private String name;
  6. private String address;
  7.  
  8. public String getName() {
  9. return name;
  10. }
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14. public String getAddress() {
  15. return address;
  16. }
  17. public void setAddress(String address) {
  18. this.address = address;
  19. }
  20.  
  21. }

MainActivity.java:

  1. package com.andyidea.listview;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6.  
  7. import com.andyidea.bean.Person;
  8.  
  9. import android.app.Activity;
  10. import android.app.AlertDialog;
  11. import android.content.Context;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.view.LayoutInflater;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.widget.BaseAdapter;
  18. import android.widget.Button;
  19. import android.widget.CheckBox;
  20. import android.widget.ListView;
  21. import android.widget.TextView;
  22.  
  23. public class MainActivity extends Activity {
  24.  
  25. Button show;
  26. ListView lv;
  27. List<Person> persons = new ArrayList<Person>();
  28. Context mContext;
  29. MyListAdapter adapter;
  30. List<Integer> listItemID = new ArrayList<Integer>();
  31.  
  32. /** Called when the activity is first created. */
  33. @Override
  34. public void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.main);
  37. mContext = getApplicationContext();
  38. show = (Button)findViewById(R.id.show);
  39. lv = (ListView)findViewById(R.id.lvperson);
  40.  
  41. initPersonData();
  42. adapter = new MyListAdapter(persons);
  43. lv.setAdapter(adapter);
  44.  
  45. show.setOnClickListener(new View.OnClickListener() {
  46.  
  47. @Override
  48. public void onClick(View v) {
  49.  
  50. listItemID.clear();
  51. for(int i=0;i<adapter.mChecked.size();i++){
  52. if(adapter.mChecked.get(i)){
  53. listItemID.add(i);
  54. }
  55. }
  56.  
  57. if(listItemID.size()==0){
  58. AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
  59. builder1.setMessage("没有选中任何记录");
  60. builder1.show();
  61. }else{
  62. StringBuilder sb = new StringBuilder();
  63.  
  64. for(int i=0;i<listItemID.size();i++){
  65. sb.append("ItemID="+listItemID.get(i)+" . ");
  66. }
  67. AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
  68. builder2.setMessage(sb.toString());
  69. builder2.show();
  70. }
  71. }
  72. });
  73. }
  74.  
  75. /**
  76. * 模拟数据
  77. */
  78. private void initPersonData(){
  79. Person mPerson;
  80. for(int i=1;i<=12;i++){
  81. mPerson = new Person();
  82. mPerson.setName("Andy"+i);
  83. mPerson.setAddress("GuangZhou"+i);
  84. persons.add(mPerson);
  85. }
  86. }
  87.  
  88. //自定义ListView适配器
  89. class MyListAdapter extends BaseAdapter{
  90. List<Boolean> mChecked;
  91. List<Person> listPerson;
  92. HashMap<Integer,View> map = new HashMap<Integer,View>();
  93.  
  94. public MyListAdapter(List<Person> list){
  95. listPerson = new ArrayList<Person>();
  96. listlistPerson = list;
  97.  
  98. mChecked = new ArrayList<Boolean>();
  99. for(int i=0;i<list.size();i++){
  100. mChecked.add(false);
  101. }
  102. }
  103.  
  104. @Override
  105. public int getCount() {
  106. return listPerson.size();
  107. }
  108.  
  109. @Override
  110. public Object getItem(int position) {
  111. return listPerson.get(position);
  112. }
  113.  
  114. @Override
  115. public long getItemId(int position) {
  116. return position;
  117. }
  118.  
  119. @Override
  120. public View getView(int position, View convertView, ViewGroup parent) {
  121. View view;
  122. ViewHolder holder = null;
  123.  
  124. if (map.get(position) == null) {
  125. Log.e("MainActivity","position1 = "+position);
  126.  
  127. LayoutInflater mInflater = (LayoutInflater) mContext
  128. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  129. view = mInflater.inflate(R.layout.listitem, null);
  130. holder = new ViewHolder();
  131. holder.selected = (CheckBox)view.findViewById(R.id.list_select);
  132. holder.name = (TextView)view.findViewById(R.id.list_name);
  133. holder.address = (TextView)view.findViewById(R.id.list_address);
  134. final int p = position;
  135. map.put(position, view);
  136. holder.selected.setOnClickListener(new View.OnClickListener() {
  137.  
  138. @Override
  139. public void onClick(View v) {
  140. CheckBox cb = (CheckBox)v;
  141. mChecked.set(p, cb.isChecked());
  142. }
  143. });
  144. view.setTag(holder);
  145. }else{
  146. Log.e("MainActivity","position2 = "+position);
  147. view = map.get(position);
  148. holder = (ViewHolder)view.getTag();
  149. }
  150.  
  151. holder.selected.setChecked(mChecked.get(position));
  152. holder.name.setText(listPerson.get(position).getName());
  153. holder.address.setText(listPerson.get(position).getAddress());
  154.  
  155. return view;
  156. }
  157.  
  158. }
  159.  
  160. static class ViewHolder{
  161. CheckBox selected;
  162. TextView name;
  163. TextView address;
  164. }
  165. }

运行结果:

Android中ListView结合CheckBox判断选中项的更多相关文章

  1. Android中Listview点击item不变颜色以及设置listselector 无效

    Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...

  2. Android中ListView控件的使用

    Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...

  3. android中ListView控件&&onItemClick事件中获取listView传递的数据

    http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...

  4. android中ListView点击和里边按钮点击不能同时生效问题解决

    今天遇到一个问题:android中ListView点击和里边button点击不能同时生效问题解决. 原因是: listView 在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得 ...

  5. Android中ListView无法点击

    Android中ListView无法点击 转自:http://xqjay19910131-yahoo-cn.iteye.com/blog/1319502   问题描述: ListView中Item加入 ...

  6. Android中 ListView 详解(二)

    本文版权归 csdn noTice501 所有,转载请详细标明原作者及出处,以示尊重! 作者:noTice501 原文:http://blog.csdn.net/notice520/article/d ...

  7. android中listview分页载入数据

    前段时间做的新浪微博项目一直想实现listview分页载入数据,今天最终实现了,哈哈!感觉挺好的,今天又写了个demo给大家分享下. 首先说下listview的优化方案,这也是面试中常考的题目.优化方 ...

  8. Android中ListView的几种常见的优化方法

    Android中的ListView应该算是布局中几种最常用的组件之一了,使用也十分方便,下面将介绍ListView几种比较常见的优化方法: 首先我们给出一个没有任何优化的Listview的Adapte ...

  9. android中listview的item滑动删除效果(已解决listview点击问题)

    领导看到iphone上tableview有个滑动删除的效果,要求在android上也实现,搜了下资料,实现起来比较简单,可弄到后面,居然不能点击了,把一篇文章中的代码修改了一下,捣鼓了一番,搞定,下面 ...

随机推荐

  1. C# MD5加密-MD5Helper

    原文地址:https://ken.io/note/csharp-md5 一.前言 MD5说明http://zh.wikipedia.org/wiki/MD5 .NET MD5类 官方文档&示例 ...

  2. Mongodb 分片原理

    1.主从mongodb 模式 类似,MySQL的主从配置  参照:https://blog.csdn.net/liusong0605/article/details/11551699 mongoDB有 ...

  3. Python爬虫:数据解析 之 xpath

    资料: W3C标准:https://www.w3.org/TR/xpath/all/ W3School:https://www.w3school.com.cn/xpath/index.asp 菜鸟教程 ...

  4. ubuntu资料

    1.VNC实现Windows远程访问Ubuntu 16.04(无需安装第三方桌面,直接使用自带远程工具) https://www.cnblogs.com/xuliangxing/p/7642650.h ...

  5. mxgraph初体验

    最近公司让学习了mxgraph,简单总结一下 (1)mxGraph学习路径 1)API:http://jgraph.github.io/mxgraph/docs/js-api/files/index- ...

  6. Linux vim中方向键变成字母的问题

    使用Ubuntu Desktop 18.04 时 发现 vim 在编辑模式的时候,方向键变成了字母ABCD. 原因: Ubuntu预装的是vim tiny版本,安装vim full版本即可解决. 1. ...

  7. oracle的decode、sign、nvl,case...then函数

    ORACLE几种常用的方法 1.decode 常见的用法 : 格式:decode(condition,value1,result[, value2,result2], default_result) ...

  8. Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime

    环境: ubuntu18 webstorm vue项目 报错原因: 缺少相关依赖 解决方法: npm rebuild node-sass 还未解决: npm uninstall --save node ...

  9. 汇编语言从入门到精通-5微机CPU的指令系统2

    微机CPU的指令系统 5.2.2 标志位操作指令 标志位操作指令是一组对标志位置位.复位.保存和恢复等操作的指令. 1.进位CF操作指令 a.清进位指令CLC(Clear Carry Flag):CF ...

  10. js运动框架及应用

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...