开发者

Updating Contact causing wrong values to be written

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

    t开发者_如何学Gory {
        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();

    }
}


see this link:

http://comments.gmane.org/gmane.comp.handhelds.android.devel/92848

" you shouldn't specify the mime type in an update statement - it is not updatable."

"Your query says: "update all data rows for this contact, setting one of the fields to "John" and another to "Abraham". Data rows include phone numbers, emails, photos, you-name-it. What you need to do is find a specific data row you want to update and then use its _id in the selection (or, more commonly, in the URI itself)."

"The data structure for contacts is based on three separate tables: Contacts, RawContacts and Data. A Contact can have multiple RawContacts, which can have multiple Data rows. Each data row has a mime type that specifies what kind of data is stored in that row. Contact name is stored in a row with the mime type StructuredName.CONTENT_TYPE.

So, in order to change the contact name you first need to find the Data row that contains the name and then update that Data row. The provider will take care of the rest (e.g. promoting the name to the level of RawContact and then Contact)."


Also see

http://www.eoeandroid.com/sdk/api/reference/android/provider/ContactsContract.Data.html

must get the dataId first :

ArrayList ops = Lists.newArrayList(); ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) .withSelection(Data._ID + "=?", new String[]{String.valueOf(dataId)}) .withValue(Email.DATA, "somebody@android.com") .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜