Android中ListView与CheckBox结合----多选与记录

来源:互联网 发布:剑三成男脸型数据大全 编辑:程序博客网 时间:2024/05/20 06:26

在实际项目中用到了这个功能,所以查了众多同行的帖子,,发现这位小伙伴的最符合我的需求,所以转载过来。实际应用中我还加入了一些个人的特殊处理,如果有问题大家可以在评论区留言交流。



很多时候我们会用到ListView与CheckBox结合的东西,比如一个清单,可以用来多选,但是很多人似乎也在这个地方遇到很多问题,我刚开始学的时候也是遇到假选问题,当列表中数量多的之后,我勾选一个,滑动页面会发现条目也勾选上了, 这明显与我们的要求不符合,后来网上找了找资料,用HashMap来记录一个CheckBox的勾选记录就解决了

以下是实现的的一个小Demo



这是XML ListView 每个item文件清单

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <RelativeLayout  
  8.         android:id="@+id/outpatient_check_hospital"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginBottom="5.0dip"  
  12.         android:layout_marginLeft="12.599976dip"  
  13.         android:layout_marginRight="12.599976dip"  
  14.         android:layout_marginTop="5.0dip"  
  15.         android:gravity="center_vertical"   
  16.         android:background="#AAAAAA">  
  17.   
  18.         <LinearLayout  
  19.             android:id="@+id/linear_layout_up"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="wrap_content"  
  22.             android:layout_margin="10.0dip"  
  23.             android:gravity="center"  
  24.             android:orientation="horizontal" >  
  25.   
  26.             <ImageView  
  27.                 android:layout_width="10dip"  
  28.                 android:layout_height="10dip"  
  29.                 android:adjustViewBounds="false" />  
  30.   
  31.             <TextView  
  32.                 android:id="@+id/tv_device_name"  
  33.                 android:layout_width="wrap_content"  
  34.                 android:layout_height="wrap_content"  
  35.                 android:layout_weight="2.0"  
  36.                 android:text="名称"  
  37.                 android:textColor="#ff323232"  
  38.                 android:textSize="16.0sp"  
  39.                 android:typeface="monospace" />  
  40.   
  41.             <CheckBox  
  42.                 android:id="@+id/checkBox1"  
  43.                 android:layout_width="wrap_content"  
  44.                 android:layout_height="wrap_content"  
  45.                 android:text="" />  
  46.               
  47.         </LinearLayout>  
  48.     </RelativeLayout>  
  49.   
  50. </LinearLayout>  




这是显示ListView的页面,简单初始化几个数据

[java] view plain copy
  1. public class MainActivity extends Activity {  
  2.   
  3.     private ListView listView;  
  4.     private ListViewAdapter adapter;  
  5.     private String[] beans = new String[] { "1""2""3""4""5""6""7",  
  6.             "8""9""10""11""12""13","14","15","16","17","18","19" };  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.   
  13.         initView();  
  14.     }  
  15.       
  16.     private void initView() {  
  17.         // TODO Auto-generated method stub  
  18.         Log.i("htp""beans.size:" + beans.length);  
  19.         listView = (ListView) findViewById(R.id.listView1);  
  20.         adapter = new ListViewAdapter(MainActivity.this, beans);  
  21.         listView.setAdapter(adapter);  
  22.         listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);  
  23.     }  



下面就写一个Adapter类,我们依然继承BaseAdapter类。这里我们使用一个HashMap<Integer,boolean>的键值来记录checkbox在对应位置的选中状况

[java] view plain copy
  1. package com.example.listviewcheckboxdemo;  
  2.   
  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.   
  12. public class ListViewAdapter extends BaseAdapter {  
  13.   
  14.     private Context context;  
  15.     private String[] beans;  
  16.   
  17.     // 用来控制CheckBox的选中状况  
  18.     private static HashMap<Integer, Boolean> isSelected;  
  19.   
  20.     class ViewHolder {  
  21.   
  22.         TextView tvName;  
  23.         CheckBox cb;  
  24.     }  
  25.   
  26.     public ListViewAdapter(Context context, String[] beans) {  
  27.         // TODO Auto-generated constructor stub  
  28.         this.beans = beans;  
  29.         this.context = context;  
  30.         isSelected = new HashMap<Integer, Boolean>();  
  31.         // 初始化数据  
  32.         initDate();  
  33.     }  
  34.   
  35.     // 初始化isSelected的数据  
  36.     private void initDate() {  
  37.         for (int i = 0; i < beans.length; i++) {  
  38.             getIsSelected().put(i, false);  
  39.         }  
  40.     }  
  41.   
  42.     @Override  
  43.     public int getCount() {  
  44.         // TODO Auto-generated method stub  
  45.         return beans.length;  
  46.     }  
  47.   
  48.     @Override  
  49.     public Object getItem(int position) {  
  50.         // TODO Auto-generated method stub  
  51.         return beans[position];  
  52.     }  
  53.   
  54.     @Override  
  55.     public long getItemId(int position) {  
  56.         // TODO Auto-generated method stub  
  57.         return position;  
  58.     }  
  59.   
  60.     @Override  
  61.     public View getView(final int position, View convertView, ViewGroup parent) {  
  62.         // TODO Auto-generated method stub  
  63.         // 页面  
  64.         ViewHolder holder;  
  65.         String bean = beans[position];  
  66.         LayoutInflater inflater = LayoutInflater.from(context);  
  67.         if (convertView == null) {  
  68.             convertView = inflater.inflate(  
  69.                     R.layout.assist_device_binding_list_item, null);  
  70.             holder = new ViewHolder();  
  71.             holder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1);  
  72.             holder.tvName = (TextView) convertView  
  73.                     .findViewById(R.id.tv_device_name);  
  74.             convertView.setTag(holder);  
  75.         } else {  
  76.             // 取出holder  
  77.             holder = (ViewHolder) convertView.getTag();  
  78.         }  
  79.   
  80.         holder.tvName.setText(bean);  
  81.         // 监听checkBox并根据原来的状态来设置新的状态  
  82.         holder.cb.setOnClickListener(new View.OnClickListener() {  
  83.   
  84.             public void onClick(View v) {  
  85.   
  86.                 if (isSelected.get(position)) {  
  87.                     isSelected.put(position, false);  
  88.                     setIsSelected(isSelected);  
  89.                 } else {  
  90.                     isSelected.put(position, true);  
  91.                     setIsSelected(isSelected);  
  92.                 }  
  93.   
  94.             }  
  95.         });  
  96.   
  97.         // 根据isSelected来设置checkbox的选中状况  
  98.         holder.cb.setChecked(getIsSelected().get(position));  
  99.         return convertView;  
  100.     }  
  101.   
  102.     public static HashMap<Integer, Boolean> getIsSelected() {  
  103.         return isSelected;  
  104.     }  
  105.   
  106.     public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {  
  107.         ListViewAdapter.isSelected = isSelected;  
  108.     }  
  109. }  

需要的朋友直接拷贝修改就可以了


 

0 0
原创粉丝点击