Shibin Francis Shibin Francis - 1 year ago 153
Android Question

How to delete a particular contact using contact id?

I am trying to delete a particular contact from phone. I can delete the full contact. How to delete a particular contact using contact id. I want to delete the full datas including firstname, last name ,phone number, email, notes, etc...

Answer Source

try the following code:

            final ArrayList ops = new ArrayList();
        final ContentResolver cr = getContentResolver();
                                + " = ?",
                        new String[] { selected_contact_IDfromlist })
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Delete This Contact!");
        alertDialog.setMessage("Are you Sure you want to delete this contact?");
        alertDialog.setButton(getString(R.string.callLog_delDialog_yes), new DialogInterface.OnClickListener() {    // DEPRECATED
          public void onClick(DialogInterface dialog, int which) {
            try {
                cr.applyBatch(ContactsContract.AUTHORITY, ops);
            } catch (OperationApplicationException e) {
                // TODO Auto-generated catch block

            } catch (RemoteException e) {
                // System.out.println(" length :"+i);
        } }); 
        alertDialog.setButton2(getString(R.string.callLog_delDialog_no), (DialogInterface.OnClickListener)null);    // DEPRECATED
        try {
        }catch(Exception e) {
            //              Log.e(THIS_FILE, "error while trying to show deletion yes/no dialog");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download