Im trying to add a phone number to an already existing contact on a Droid-phone. Doing it at the same time as I create a contact is trivial, as the backreference I supply simply is 0 when creating a ContentProviderOperation. But trying to find the backreference through querying for a display name like this does not work:
Cursor rawContactsReferenceCursor =
contentResolver.query(Data.CONTENT_URI,
new String[]{Data.RAW_CONTACT_ID},
Data.DISPLAY_NAME+"=\""+displayName+"\"", null, null);
While I do get a raw contact ID, the following code just generates an IndexOutOfBoundException (rawConcactReferenceID is the variable I got from the previous query):
ArrayList<ContentProviderOperation> op_list =
new ArrayList<ContentProviderOperation>();
op_list.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawConcactReferenceID)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, testNumber)
.withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
.withValue(Phone.LABEL, testLabel)
.build());
ContentProviderResult[] result =
contentResolver.applyBatch(ContactsContract.AUTHORITY, op_list);
The big challenge is a huge lack of good documentation. I would be very satisfied to just get my hands on some working copypasta to study.
Cheers,
These links may provide some help:
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/1/
http://developer.android.com/resources/samples/ContactManager/src/com/example/android/contactmanager/ContactAdder.html
I've had a similar problem with email addresses. Here's the solution I used that worked:
The same solution should work for telephone numbers.
I found an answer. It is not atomic if you want to add several things right away, but hey, who needs stupid atomicity?