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

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


应用开发中经常会有从数据库中读取数据显示,然后选中多条、全部记录并且删除的需求。在做定制系统联系人的时候也遇到这样的需求,下面写个简单的通过ListView和CheckBox实现多选、全选的例子。下面是具体的代码,有问题请留言。代码下载地址:MultiSelect.rar
基于ListView和CheckBox实现多选和全选记录的功能_第1张图片
 
MultiSelectActivity
[java] view plain copy
  1. /** 
  2.  * @author [email protected]  
  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 selectedMap;  
  58.         HashSet delContactsIdSet;  
  59.   
  60.         public ContactsDeleteAdapter(Context context, Cursor c) {  
  61.             cur = c;  
  62.             // 保存每条记录是否被选中的状态  
  63.             selectedMap = new HashMap();  
  64.             // 保存被选中记录作数据库表中的Id  
  65.             delContactsIdSet = new HashSet();  
  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 task = new AsyncTask() {  
  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
[html] view plain copy
  1. xml version="1.0" encoding="utf-8"?>  

你可能感兴趣的:(android)