Application development often has the need to read data from a database display, and then select multiple, full records and deletions. When you're working on a custom system contact, here's a simple example of ListView and checkbox implementations for multiple selections and all selections. The following is the specific code.
The effect is as follows:
Multiselectactivity
/** * multiselectactivity/public class Multiselectactivity extends activity implements Onclicklistener, Onit
Emclicklistener {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 ();
}//Query database private void RefreshData () {cursor = Getcontentresolver (). Query (Contacts.content_uri, projection,
NULL, 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;
Saves the status of each record being selected Selectedmap = new Hashmap<integer, boolean> ();
Save the selected record as id delcontactsidset = new hashset<string> () in the database table;
for (int i = 0; i < Cur.getcount (); i++) {Selectedmap.put (I, false); @Override public View getview (int position, View Convertview, ViewGroup parent) {if (CONVERTV Iew = = null) {Convertview = Layoutinflater.from (multiselectactivity.this). Inflate (r.layout.contact
S_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)); Save Record ID if (selectedmap.get (position)) {Delcontactsidset.add (string.valueof cur. getInt (CO
Ntacts_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 C
ur
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://Select all CheckBox Click event Handling if (selectall.ischecked ()) {for (int i = 0; ; Contactsdeleteadapter.getcount ();
i++) {ContactsDeleteAdapter.selectedMap.put (I, true);
Click event: All selected checkbox is checked to add all contact ID to Delcontactsidset contactsDeleteAdapter.cur.moveToPosition (i); ContactsDeleteAdapter.delContactsIdSet.add (String. valueof (contactsdeleteadapter.cur. GE
TInt (Contacts_id_index)));
} contactsdelbtn.setenabled (True); else {for (int i = 0; i < Contactsdeleteadapter.getcount (); i++) {contactsdeleteadapter.selecte
Dmap.put (I, false);
//click event: All selected checkbox is canceled check delcontactsidset empty 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.clearconfirma tion). Setnegativebutton (Android. R.string.cancel, NULL). Setpositivebutton (Android. R.string.ok, New Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterfa
Ce 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, void> () {@Override
protected void Doinbackground (void ... params) {//Delete record event handling (I don't write detailed code here)
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://Cancel event handling 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 (); Determine if a record is not selected in order to modify the Select checkbox Check state if (ContactsDeleteAdapter.selectedMap.containsValue (false)) {SELECTALL.SETC
Hecked (FALSE);
else {selectall.setchecked (true); //Determine if any records are selected to set the Delete button to be available if (contactsdeleteadapter.selecteDmap.containsvalue (True) {contactsdelbtn.setenabled (true);
else {contactsdelbtn.setenabled (false);
}
}
}
Layout file: 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:layou
t_weight= "1"/> <linearlayout style= "@android: Style/buttonbar" 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:layo ut_weight= "1" android:enabled= "False "android:text=" @string/delete "/> <button android:id=" @+id/contacts_cancel_btn " Droid:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text
= "@string/cancel"/> </LinearLayout> </LinearLayout>
Code download address: Source download
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.