The implementation method of deletion function of Android Address Book Development _android

Source: Internet
Author: User
Tags message queue

Whether it is Android development or other mobile platform development, ListView must be a big coffee, then the operation of the ListView is certainly not less, the last blog on how to achieve the full selection and the selection of functions, this blog introduces the deletion function, Delete items in the list is not a timely refresh list, which is related to the UI thread, or that sentence, the data update notice must be done on the UI thread, or there will be a variety of errors, such as the occurrence of adapter data source changes, but did not receive timely notification of the situation. In the execution of traversal Delete, it is best not to delete a direct notice, the following is my implementation method, will need to delete the contact to save to a list and then through the handler message, and then by the handler to deal with.

Here I'll mention the implementation of the menu in passing.
1. Define the XML file under menu

Copy Code code as follows:

<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >

<group android:id= "@+id/metting_menu" >
<item
Android:id= "@+id/select_all_item"
android:icon= "@drawable/select_all_not"
android:title= "Select All" >
</item>
<item
Android:id= "@+id/delete_item"
android:icon= "@drawable/delete"
android:title= "Delete" >
</item>
<item
Android:id= "@+id/sync_item"
android:icon= "@drawable/sync_manage"
android:title= "Sync" >
</item>
</group>

</menu>


Icon for menu icons
Title as menu text
2. Code implementation
Copy Code code as follows:

/**
* Create Menu
*/
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Menuinflater inflater = Getmenuinflater ();
Inflater.inflate (R.menu.meetting_contact_menu, menu);
Selectallitem = Menu.finditem (R.id.select_all_item);
return true;
}

Triggered when the user opens the menu
@Override
public boolean onmenuopened (int featureid, menu menu) {
if (menu!= null) {
Refreshselectallitemicon ();
}
Return super.onmenuopened (Featureid, menu);
}

/**
* Click event for Menu
*/
@Override
public boolean onoptionsitemselected (MenuItem item) {
Switch (Item.getitemid ()) {
Case R.id.select_all_item:
if (isallchecked) {//anti-election
Item.seticon (R.drawable.select_all_not);
Invertselection ();
else {//All selected
if (contactlist.size () = = 0) {
Toast.maketext (This, "there is no alternative contact", Toast.length_short)
. Show ();
Break
}
Item.seticon (R.drawable.select_all);
Selectallcontacts ();
}
Break
Case R.id.delete_item://delete
if (count = = 0) {
Toast.maketext (This, "You have not selected any contacts", Toast.length_short). Show ();
Break
}
Showdeletedialog (This, "Are you sure you want to delete it?");
isallchecked = false;
Selectallitem.seticon (R.drawable.select_all_not);
Break
Case R.id.sync_item:
Intent fresh_intent = new Intent (this, cloudsyncactivity.class);
if (flag) {//If it is a meeting
Fresh_intent.putextra ("number", Meetingnumber);
}
Startactivityforresult (Fresh_intent, Cloud_sync);
Break
Default
Break
}
return false;
}

Refresh menu item Select All
private void Refreshselectallitemicon () {
For (String ContactId:map_NumberSelected.keySet ()) {
if (!map_numberselected.get (ContactID)) {
isallchecked = false;
Selectallitem.seticon (R.drawable.select_all_not);
Break
}
Isallchecked = true;
Selectallitem.seticon (R.drawable.select_all);
}
}


Prompt for deleted dialog box
Copy Code code as follows:

/** Show Confirmation Delete dialog box **/
public void Showdeletedialog (context context, String msg) {
Final Dialog Dialog = new Dialog (context, r.style.mydialog);
View view = Layoutinflater.from (context). Inflate (R.layout.back_dialog,
NULL);
TextView MSG_TV = (TextView) View.findviewbyid (r.id.msg);
Msg_tv.settext (msg);
button sure = (Button) View.findviewbyid (r.id.sure);
Button cancle = (button) View.findviewbyid (r.id.cancle);
Dialog.setcontentview (view);
Sure.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Deletecontacts ();
Dialog.dismiss ();
}
});
Cancle.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
Dialog.dismiss ();
}
});
Dialog.show ();
}


The following is a delete operation:
Here only focus on implementing the deletion of the pulse path, some of which are business logic needs to be able to look at a little bit of it
Copy Code code as follows:

Delete the selected contacts
public void Deletecontacts () {
Dialoghelp.getinstance (). Showhttpdialog (This, r.string.wait_for_while,
GetString (r.string.deleting_contact));
New Thread () {
@Override
public void Run () {
arraylist<contact> deletecontacts = new arraylist<contact> ();
for (contact contact:contactlist) {
Boolean isselected = map_numberselected
. Get (Contact.getid ());
if (isselected) {
for (Rawcontact rawcontact:rawlist) {
if (contact.getuserid () = = Rawcontact.getuserid ()) {//is the same ID
if (contact.getnumber () = = Rawcontact
. GetPhone1 ()) {
RAWCONTACT.SETPHONE1 (NULL);
else if (contact.getnumber () = = Rawcontact
. GetPhone2 ()) {
Rawcontact.setphone2 (NULL);
else if (contact.getnumber () = = Rawcontact
. Gettelephone ()) {
Rawcontact.settelephone (NULL);
}
if (rawcontact.getphone1 () = = NULL | | ""
. Equals (Rawcontact.getphone1 ()))
&& (rawcontact.getphone2 () = NULL | | ""
. Equals (Rawcontact.getphone2 ()))
&& (rawcontact.gettelephone () = NULL | | ""
. Equals (Rawcontact
. Gettelephone ())) {
Mydatabase.deletecontact (
Meetingcontactactivity.this,
Rawcontact);
} else {
Mydatabase.updatecontactbyid (
Meetingcontactactivity.this,
Rawcontact);
}
Break
}
}
Save contacts that need to be deleted
Deletecontacts.add (contact);
}
}
msg = new Message ();
Msg.obj = deletecontacts;
Msg.what = Delete_complete;
Handler.sendmessage (msg);
};
}.start ();

}


The deletecontacts is used to save the contact that needs to be deleted, assign it to Msg.obj, and then handler the message into the message queue and eventually delete the handler Handlemessage method.


Handler Processing code:
Here it passes over the list in ContactList traversal deletes, and then refreshes the notification list in msg.what for the Delete_complete branch.

Copy Code code as follows:

Private Handler Handler = new Handler () {
@Override
public void Handlemessage (msg) {
if (msg.obj!= null) {
Arraylist<contact> deletecontacts = (arraylist<contact>) msg.obj;
for (contact contact:deletecontacts) {
Map_numberselected.remove (Contact.getid ());
Contactlist.remove (contact);
}
}
Switch (msg.what) {
Case load_contact_finished:
To determine if it is empty
if (contactlist = null | | contactlist.size () = 0) {
Synclayout.setvisibility (view.visible);
Bottomlayout.setvisibility (View.gone);
Listview.setvisibility (View.gone);
} else {
Synclayout.setvisibility (View.gone);
Bottomlayout.setvisibility (view.visible);
Listview.setvisibility (view.visible);
}
Adapter.setcontactlist (contactlist);
Refreshlist ();
Dialoghelp.getinstance (). DismissDialog ();
Break
Case Cloud_sync://Sync
Initcontacts ();
Refreshlist ();
Break
Case Search_finish:
Refreshlist ();
Break
Case Delete_complete://delete completed
Dialoghelp.getinstance (). DismissDialog ();
Re-initialize
Refreshlist ();
Break
}
}
};

The above is the deletion of the full implementation of the function, about the Address Book Module development blog may suspend updates, such as the alphabet Index, contact group and other functions I have not to achieve, the opportunity will continue to share.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.