How to delete a particular contact using contact id?
try the following code:
final ArrayList ops = new ArrayList();
final ContentResolver cr = getContentResolver();
ops.add(ContentProviderOperation
.newDelete(ContactsContract.RawContacts.CONTENT_URI)
.withSelection(
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?",
new String[] { selected_contact_IDfromlist })
.build());
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);
background_process();
ops.clear();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// System.out.println(" length :"+i);
}
return;
} });
alertDialog.setButton2(getString(R.string.callLog_delDialog_no), (DialogInterface.OnClickListener)null); // DEPRECATED
try {
alertDialog.show();
}catch(Exception e) {
// Log.e(THIS_FILE, "error while trying to show deletion yes/no dialog");
}
Delete a single phone type number from a contact
Each number from same contact has it's own ID. You should use it for deleting. But you also need contact ID. You can get it using this line of code:
contactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Then you delete the number using following code:
Cursor cur = contentResolver.query(RawContacts.CONTENT_URI,
new String[]{RawContacts._ID},
RawContacts.CONTACT_ID + "=?",
new String[] {contactID.toString()}, null);
int rowId=0;;
if(cur.moveToFirst()){
rowId = cur.getInt(cur.getColumnIndex(RawContacts._ID));
}
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
String selectPhone = Data.RAW_CONTACT_ID + " = ? AND " +
Data.MIMETYPE + " = ? AND " +
Phone._ID + " = ?";
String[] phoneArgs = new String[] { Integer.toString(rowId),
Phone.CONTENT_ITEM_TYPE,
ID.toString()};
ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs).build());
try {
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
Android Studio Delete contact
try this.
for delete.
public static void deleteContact(ContentResolver contactHelper, String
number) {
ArrayList<ContentProviderOperation> ops = new
ArrayList<ContentProviderOperation>();
String[] args = new String[] { String.valueOf(getContactID(contactHelper,
number))};
ops.add(ContentProviderOperatio.newDelete(RawContacts.CONTENT_URI).withSelecti
on(RawContacts.CONTACT_ID + "=?", args).build());
try {
contactHelper.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
}
and getid
private static long getContactID(ContentResolver contactHelper,String
number) {
Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] projection = { PhoneLookup._ID };
Cursor cursor = null;
try {
cursor = contactHelper.query(contactUri, projection, null, null,null);
if (cursor.moveToFirst()) {
int personID = cursor.getColumnIndex(PhoneLookup._ID);
return cursor.getLong(personID);
}
return -1;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
cursor = null;
}
}
return -1;
}
Related Topics
What Is Meant by Ems? (Android Textview)
Runonuithread VS Looper.Getmainlooper().Post in Android
Best Way to Secure Android App Sensitive Data
How to Connect with Remote Debugger
How to Add -Xlint:Unchecked to My Android Gradle Based Project
How to Get Each Device's Ip Address in Wi-Fi Direct Scenario
Android Exporting to CSV and Sending as Email Attachment
Video Streaming Using Rtsp: Android
Android 'Unable to Add Window -- Token Null Is Not for an Application' Exception
Receiver Not Registered Exception Error
Proguard Missing Type Parameter
Generating Google Map Release API Key
Inserting Contacts in Android 2.2
Bitmap, Bitmap.Recycle(), Weakreferences, and Garbage Collection
Enable and Disable Auto Rotate Programmatically