tags:

views:

70

answers:

0

I am trying to display the name, phone number and email of a contact and letting the user update any of the values. For some reason, the value of email is overwritten on all three fields when my code is executed. There's got to be a glitch in the logic here, but I cannot figure it out.

Here's my method which updates the Contacts URI.

private void updateContact() {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, name)
            .build());
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
            .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .build());
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber)
            .withValue(ContactsContract.Data.MIMETYPE, 
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .build());
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
            .build());

    try {
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        // Display update
        Context ctx = getApplicationContext();
        CharSequence txt = "Contact Updated";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(ctx, txt, duration);
        toast.show();

    } catch (Exception e) {
        // Display warning
        Context ctx = getApplicationContext();
        CharSequence txt = "Update Failed";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(ctx, txt, duration);
        toast.show();

    }
}