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

android基于ListView和CheckBox实现多选和全选记录的功能,第1张

概述应用开发中经常会有从数据库中读取数据显示,然后选中多条、全部记录并且删除的需求。在做定制系统联系人的时候也遇到这样的需求,下面写个简单的通过ListView和CheckBox实现多选、全选的例子。下面是具体的代码.

应用开发中经常会有从数据库中读取数据显示,然后选中多条、全部记录并且删除的需求。在做定制系统联系人的时候也遇到这样的需求,下面写个简单的通过ListVIEw和CheckBox实现多选、全选的例子。下面是具体的代码.

效果如下:


MultiSelectActivity

/**  * MultiSelectActivity  */ public class MultiSelectActivity extends Activity implements OnClickListener,OnItemClickListener {   private static final String TAG = "MultiSelectActivity";   private ListVIEw contactsDelList;   private button contactsDelBtn;   private button contactsCancelBtn;   private CheckBox selectAll;   private Cursor cursor;   private ContactsDeleteAdapter contactsDeleteAdapter;    private static final String[] PROJECTION = new String[] { Contacts._ID,Contacts.disPLAY_name };   private static final int CONTACTS_ID_INDEX = 0;   private static final int disPLAY_name_INDEX = 1;   private ContactsDeleteListItemVIEws holderVIEws;    private final class ContactsDeleteListItemVIEws {     TextVIEw nameVIEw;     CheckBox delCheckBox;   }    @OverrIDe   public voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentVIEw(R.layout.contacts_delete_activity);     contactsDelList = (ListVIEw) findVIEwByID(R.ID.contacts_delete_List);     contactsDelBtn = (button) findVIEwByID(R.ID.contacts_delete_btn);     contactsCancelBtn = (button) findVIEwByID(R.ID.contacts_cancel_btn);     selectAll = (CheckBox) (findVIEwByID(R.ID.contacts_delete_List_header)         .findVIEwByID(R.ID.select_all));     contactsDelList.setonItemClickListener(this);     contactsDelBtn.setonClickListener(this);     contactsCancelBtn.setonClickListener(this);     selectAll.setonClickListener(this);   }    @OverrIDe   protected voID onResume() {     super.onResume();     refreshData();   }    // 查询数据库   private voID refreshData() {     cursor = getContentResolver().query(Contacts.CONTENT_URI,PROJECTION,null,null);     contactsDeleteAdapter = new ContactsDeleteAdapter(this,cursor);     contactsDelList.setAdapter(contactsDeleteAdapter);   }    class ContactsDeleteAdapter extends BaseAdapter {     Cursor cur;     Map<Integer,Boolean> selectedMap;     HashSet<String> delContactsIDSet;      public ContactsDeleteAdapter(Context context,Cursor c) {       cur = c;       // 保存每条记录是否被选中的状态       selectedMap = new HashMap<Integer,Boolean>();       // 保存被选中记录作数据库表中的ID       delContactsIDSet = new HashSet<String>();        for (int i = 0; i < cur.getCount(); i++) {         selectedMap.put(i,false);       }     }      @OverrIDe     public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {       if (convertVIEw == null) {         convertVIEw = LayoutInflater.from(MultiSelectActivity.this)             .inflate(R.layout.contacts_delete_List_item,null);         holderVIEws = new ContactsDeleteListItemVIEws();         holderVIEws.nameVIEw = (TextVIEw) convertVIEw             .findVIEwByID(R.ID.name);         holderVIEws.delCheckBox = (CheckBox) convertVIEw             .findVIEwByID(R.ID.delete_List_item);         convertVIEw.setTag(holderVIEws);       }       cur.movetoposition(position);       ContactsDeleteListItemVIEws vIEws = (ContactsDeleteListItemVIEws) convertVIEw           .getTag();       final String name = cur.getString(disPLAY_name_INDEX);       vIEws.nameVIEw.setText(name);       vIEws.delCheckBox.setChecked(selectedMap.get(position));       // 保存记录ID       if (selectedMap.get(position)) {         delContactsIDSet.add(String.valueOf(cur             .getInt(CONTACTS_ID_INDEX)));       } else {         delContactsIDSet.remove(String.valueOf(cur             .getInt(CONTACTS_ID_INDEX)));       }       return convertVIEw;     }      @OverrIDe     public int getCount() {       return cur.getCount();     }      @OverrIDe     public Object getItem(int position) {       if (cur.movetoposition(position)) {         return cur;       } else {         return null;       }     }      @OverrIDe     public long getItemID(int position) {       return position;     }   }    @OverrIDe   public voID onClick(VIEw v) {     switch (v.getID()) {     case R.ID.select_all:       // 全选CheckBox的click事件处理       if (selectAll.isChecked()) {         for (int i = 0; i < contactsDeleteAdapter.getCount(); i++) {           contactsDeleteAdapter.selectedMap.put(i,true);           //click事件:全选checkBox被勾选则把所有的联系人ID添加到delContactsIDSet中           contactsDeleteAdapter.cur.movetoposition(i);           contactsDeleteAdapter.delContactsIDSet.add(String               .valueOf(contactsDeleteAdapter.cur                   .getInt(CONTACTS_ID_INDEX)));         }         contactsDelBtn.setEnabled(true);       } else {         for (int i = 0; i < contactsDeleteAdapter.getCount(); i++) {           contactsDeleteAdapter.selectedMap.put(i,false);         }         //click事件:全选checkBox被取消勾选则把delContactsIDSet清空         contactsDeleteAdapter.delContactsIDSet.clear();         contactsDelBtn.setEnabled(false);       }       contactsDeleteAdapter.notifyDataSetChanged();       break;     case R.ID.contacts_delete_btn:       new AlertDialog.Builder(this).setTitle(           R.string.clearConfirmation_Title).setMessage(           R.string.clearConfirmation).setNegativebutton(           androID.R.string.cancel,null).setPositivebutton(           androID.R.string.ok,new DialogInterface.OnClickListener() {             @OverrIDe             public voID onClick(DialogInterface dialog,int which) {               final ProgressDialog progressDialog = ProgressDialog                   .show(                       MultiSelectActivity.this,getString(R.string.clearProgress_Title),"",true,false);               final AsyncTask<VoID,VoID,VoID> task = new AsyncTask<VoID,VoID>() {                 @OverrIDe                 protected VoID doInBackground(VoID... params) {                   // 删除记录事件处理(我这里就不写详细的代码了)                   return null;                 }                  @OverrIDe                 protected voID onPostExecute(VoID result) {                   progressDialog.dismiss();                 }               };               progressDialog.show();               task.execute();             }           }).setCancelable(true).create().show();       break;     case R.ID.contacts_cancel_btn:       // 取消事件处理       break;     }   }    @OverrIDe   public voID onItemClick(AdapterVIEw<?> arg0,VIEw vIEw,int position,long ID) {     Log.i(TAG,"onItemClick");     ContactsDeleteListItemVIEws vIEws = (ContactsDeleteListItemVIEws) vIEw         .getTag();     vIEws.delCheckBox.toggle();     contactsDeleteAdapter.selectedMap.put(position,vIEws.delCheckBox         .isChecked());     contactsDeleteAdapter.notifyDataSetChanged();     // 判断是否有记录没被选中,以便修改全选CheckBox勾选状态     if (contactsDeleteAdapter.selectedMap.containsValue(false)) {       selectAll.setChecked(false);     } else {       selectAll.setChecked(true);     }     // 判断是否有记录被选中,以便设置删除按钮是否可用     if (contactsDeleteAdapter.selectedMap.containsValue(true)) {       contactsDelBtn.setEnabled(true);     } else {       contactsDelBtn.setEnabled(false);     }   } } 

布局文件:contacts_delete_activity.xml

<?xml version="1.0" enCoding="utf-8"?> <!--   zuolongsnail@163.com   This layout of delete contacts. --> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:orIEntation="vertical" >    <include     androID:ID="@+ID/contacts_delete_List_header"     layout="@layout/contacts_delete_List_header" />    <ListVIEw     androID:ID="@+ID/contacts_delete_List"     androID:layout_wIDth="match_parent"     androID:layout_height="0dip"     androID:layout_weight="1" />    <linearLayout          androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content"     androID:orIEntation="horizontal" >      <button       androID:ID="@+ID/contacts_delete_btn"       androID:layout_wIDth="0dip"       androID:layout_height="wrap_content"       androID:layout_weight="1"       androID:enabled="false"       androID:text="@string/delete" />      <button       androID:ID="@+ID/contacts_cancel_btn"       androID:layout_wIDth="0dip"       androID:layout_height="wrap_content"       androID:layout_weight="1"       androID:text="@string/cancel" />   </linearLayout>  </linearLayout> 

代码下载地址:源码下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的android基于ListView和CheckBox实现多选和全选记录的功能全部内容,希望文章能够帮你解决android基于ListView和CheckBox实现多选和全选记录的功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147731.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存