基于ListView和CheckBox实现多选和全选记录的功能

http://blog.csdn.net/zuolongsnail/article/details/7051620


应用开发中经常会有从数据库中读取数据显示,然后选中多条、全部记录并且删除的需求。在做定制系统联系人的时候也遇到这样的需求,下面写个简单的通过ListView和CheckBox实现多选、全选的例子。下面是具体的代码,有问题请留言。代码下载地址:MultiSelect.rar
 
MultiSelectActivity
  1. /** 
  2.  * @author zuolongsnail@163.com  
  3.  * MultiSelectActivity 
  4.  */  
  5. public class MultiSelectActivity extends Activity implements OnClickListener,  
  6.         OnItemClickListener {  
  7.     private static final String TAG = "MultiSelectActivity";  
  8.     private ListView contactsDelList;  
  9.     private Button contactsDelBtn;  
  10.     private Button contactsCancelBtn;  
  11.     private CheckBox selectAll;  
  12.     private Cursor cursor;  
  13.     private ContactsDeleteAdapter contactsDeleteAdapter;  
  14.   
  15.     private static final String[] PROJECTION = new String[] { Contacts._ID,  
  16.             Contacts.DISPLAY_NAME };  
  17.     private static final int CONTACTS_ID_INDEX = 0;  
  18.     private static final int DISPLAY_NAME_INDEX = 1;  
  19.     private ContactsDeleteListItemViews holderViews;  
  20.   
  21.     private final class ContactsDeleteListItemViews {  
  22.         TextView nameView;  
  23.         CheckBox delCheckBox;  
  24.     }  
  25.   
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.contacts_delete_activity);  
  30.         contactsDelList = (ListView) findViewById(R.id.contacts_delete_list);  
  31.         contactsDelBtn = (Button) findViewById(R.id.contacts_delete_btn);  
  32.         contactsCancelBtn = (Button) findViewById(R.id.contacts_cancel_btn);  
  33.         selectAll = (CheckBox) (findViewById(R.id.contacts_delete_list_header)  
  34.                 .findViewById(R.id.select_all));  
  35.         contactsDelList.setOnItemClickListener(this);  
  36.         contactsDelBtn.setOnClickListener(this);  
  37.         contactsCancelBtn.setOnClickListener(this);  
  38.         selectAll.setOnClickListener(this);  
  39.     }  
  40.   
  41.     @Override  
  42.     protected void onResume() {  
  43.         super.onResume();  
  44.         refreshData();  
  45.     }  
  46.   
  47.     // 查询数据库  
  48.     private void refreshData() {  
  49.         cursor = getContentResolver().query(Contacts.CONTENT_URI, PROJECTION,  
  50.                 nullnullnull);  
  51.         contactsDeleteAdapter = new ContactsDeleteAdapter(this, cursor);  
  52.         contactsDelList.setAdapter(contactsDeleteAdapter);  
  53.     }  
  54.   
  55.     class ContactsDeleteAdapter extends BaseAdapter {  
  56.         Cursor cur;  
  57.         Map<Integer, Boolean> selectedMap;  
  58.         HashSet<String> delContactsIdSet;  
  59.   
  60.         public ContactsDeleteAdapter(Context context, Cursor c) {  
  61.             cur = c;  
  62.             // 保存每条记录是否被选中的状态  
  63.             selectedMap = new HashMap<Integer, Boolean>();  
  64.             // 保存被选中记录作数据库表中的Id  
  65.             delContactsIdSet = new HashSet<String>();  
  66.   
  67.             for (int i = 0; i < cur.getCount(); i++) {  
  68.                 selectedMap.put(i, false);  
  69.             }  
  70.         }  
  71.   
  72.         @Override  
  73.         public View getView(int position, View convertView, ViewGroup parent) {  
  74.             if (convertView == null) {  
  75.                 convertView = LayoutInflater.from(MultiSelectActivity.this)  
  76.                         .inflate(R.layout.contacts_delete_list_item, null);  
  77.                 holderViews = new ContactsDeleteListItemViews();  
  78.                 holderViews.nameView = (TextView) convertView  
  79.                         .findViewById(R.id.name);  
  80.                 holderViews.delCheckBox = (CheckBox) convertView  
  81.                         .findViewById(R.id.delete_list_item);  
  82.                 convertView.setTag(holderViews);  
  83.             }  
  84.             cur.moveToPosition(position);  
  85.             ContactsDeleteListItemViews views = (ContactsDeleteListItemViews) convertView  
  86.                     .getTag();  
  87.             final String name = cur.getString(DISPLAY_NAME_INDEX);  
  88.             views.nameView.setText(name);  
  89.             views.delCheckBox.setChecked(selectedMap.get(position));  
  90.             // 保存记录Id  
  91.             if (selectedMap.get(position)) {  
  92.                 delContactsIdSet.add(String.valueOf(cur  
  93.                         .getInt(CONTACTS_ID_INDEX)));  
  94.             } else {  
  95.                 delContactsIdSet.remove(String.valueOf(cur  
  96.                         .getInt(CONTACTS_ID_INDEX)));  
  97.             }  
  98.             return convertView;  
  99.         }  
  100.   
  101.         @Override  
  102.         public int getCount() {  
  103.             return cur.getCount();  
  104.         }  
  105.   
  106.         @Override  
  107.         public Object getItem(int position) {  
  108.             if (cur.moveToPosition(position)) {  
  109.                 return cur;  
  110.             } else {  
  111.                 return null;  
  112.             }  
  113.         }  
  114.   
  115.         @Override  
  116.         public long getItemId(int position) {  
  117.             return position;  
  118.         }  
  119.     }  
  120.   
  121.     @Override  
  122.     public void onClick(View v) {  
  123.         switch (v.getId()) {  
  124.         case R.id.select_all:  
  125.             // 全选CheckBox的click事件处理  
  126.             if (selectAll.isChecked()) {  
  127.                 for (int i = 0; i < contactsDeleteAdapter.getCount(); i++) {  
  128.                     contactsDeleteAdapter.selectedMap.put(i, true);  
  129.                     //click事件:全选checkbox被勾选则把所有的联系人ID添加到delContactsIdSet中  
  130.                     contactsDeleteAdapter.cur.moveToPosition(i);  
  131.                     contactsDeleteAdapter.delContactsIdSet.add(String  
  132.                             .valueOf(contactsDeleteAdapter.cur  
  133.                                     .getInt(CONTACTS_ID_INDEX)));  
  134.                 }  
  135.                 contactsDelBtn.setEnabled(true);  
  136.             } else {  
  137.                 for (int i = 0; i < contactsDeleteAdapter.getCount(); i++) {  
  138.                     contactsDeleteAdapter.selectedMap.put(i, false);  
  139.                 }  
  140.                 //click事件:全选checkbox被取消勾选则把delContactsIdSet清空  
  141.                 contactsDeleteAdapter.delContactsIdSet.clear();  
  142.                 contactsDelBtn.setEnabled(false);  
  143.             }  
  144.             contactsDeleteAdapter.notifyDataSetChanged();  
  145.             break;  
  146.         case R.id.contacts_delete_btn:  
  147.             new AlertDialog.Builder(this).setTitle(  
  148.                     R.string.clearConfirmation_title).setMessage(  
  149.                     R.string.clearConfirmation).setNegativeButton(  
  150.                     android.R.string.cancel, null).setPositiveButton(  
  151.                     android.R.string.ok, new DialogInterface.OnClickListener() {  
  152.                         @Override  
  153.                         public void onClick(DialogInterface dialog, int which) {  
  154.                             final ProgressDialog progressDialog = ProgressDialog  
  155.                                     .show(  
  156.                                             MultiSelectActivity.this,  
  157.                                             getString(R.string.clearProgress_title),  
  158.                                             ""truefalse);  
  159.                             final AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {  
  160.                                 @Override  
  161.                                 protected Void doInBackground(Void... params) {  
  162.                                     // 删除记录事件处理(我这里就不写详细的代码了)  
  163.                                     return null;  
  164.                                 }  
  165.   
  166.                                 @Override  
  167.                                 protected void onPostExecute(Void result) {  
  168.                                     progressDialog.dismiss();  
  169.                                 }  
  170.                             };  
  171.                             progressDialog.show();  
  172.                             task.execute();  
  173.                         }  
  174.                     }).setCancelable(true).create().show();  
  175.             break;  
  176.         case R.id.contacts_cancel_btn:  
  177.             // 取消事件处理  
  178.             break;  
  179.         }  
  180.     }  
  181.   
  182.     @Override  
  183.     public void onItemClick(AdapterView<?> arg0, View view, int position,  
  184.             long id) {  
  185.         Log.i(TAG, "onItemClick");  
  186.         ContactsDeleteListItemViews views = (ContactsDeleteListItemViews) view  
  187.                 .getTag();  
  188.         views.delCheckBox.toggle();  
  189.         contactsDeleteAdapter.selectedMap.put(position, views.delCheckBox  
  190.                 .isChecked());  
  191.         contactsDeleteAdapter.notifyDataSetChanged();  
  192.         // 判断是否有记录没被选中,以便修改全选CheckBox勾选状态  
  193.         if (contactsDeleteAdapter.selectedMap.containsValue(false)) {  
  194.             selectAll.setChecked(false);  
  195.         } else {  
  196.             selectAll.setChecked(true);  
  197.         }  
  198.         // 判断是否有记录被选中,以便设置删除按钮是否可用  
  199.         if (contactsDeleteAdapter.selectedMap.containsValue(true)) {  
  200.             contactsDelBtn.setEnabled(true);  
  201.         } else {  
  202.             contactsDelBtn.setEnabled(false);  
  203.         }  
  204.     }  
  205. }  

布局文件:contacts_delete_activity.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--  
  3.     zuolongsnail@163.com  
  4.     This layout of delete contacts.  
  5. -->  
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  7.     android:layout_width="match_parent"  
  8.     android:layout_height="match_parent"  
  9.     android:orientation="vertical" >  
  10.   
  11.     <include  
  12.         android:id="@+id/contacts_delete_list_header"  
  13.         layout="@layout/contacts_delete_list_header" />  
  14.   
  15.     <ListView  
  16.         android:id="@+id/contacts_delete_list"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="0dip"  
  19.         android:layout_weight="1" />  
  20.   
  21.     <LinearLayout  
  22.         style="@android:style/ButtonBar"  
  23.         android:layout_width="match_parent"  
  24.         android:layout_height="wrap_content"  
  25.         android:orientation="horizontal" >  
  26.   
  27.         <Button  
  28.             android:id="@+id/contacts_delete_btn"  
  29.             android:layout_width="0dip"  
  30.             android:layout_height="wrap_content"  
  31.             android:layout_weight="1"  
  32.             android:enabled="false"  
  33.             android:text="@string/delete" />  
  34.   
  35.         <Button  
  36.             android:id="@+id/contacts_cancel_btn"  
  37.             android:layout_width="0dip"  
  38.             android:layout_height="wrap_content"  
  39.             android:layout_weight="1"  
  40.             android:text="@string/cancel" />  
  41.     </LinearLayout>  
  42.   
  43. </LinearLayout>  

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值