Fujitina Fujitina - 1 month ago 6
Android Question

Delete a single phone type number from a contact

When a one of the contact have multiple numbers, for example:

Display name: GuessWho
TYPE = home
Number = homeNumber

TYPE = mobile
Number = mobileNumber
TYPE = other

Number = otherNumber ...

in conclusion ... one from those.

How I can remove a TYPE with number from this contact ( let's say "mobile" )? I have to update it using the userID acquired from the previous query, or how?
I just need to delete a single TYPE with number, other field of the contact must remain intact.

I am using this piece of code for obtaining contact :

int indexName = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = c
int indexType = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int indexID = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);

String name = c.getString(indexName);
String number = c.getString(indexNumber);
String type = c.getString(indexType);
String typeStored = (String) Phone.getTypeLabel(mContext.getResources(), Integer.parseInt(type), "");
Log.i("TYPE READED : ", typeStored);
String id = c.getString(indexID);

where c is the cursor of the query.

Answer Source

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;;
        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), 

            .withSelection(selectPhone, phoneArgs).build());
    try {
        contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {
    } catch (OperationApplicationException e) {