Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

update contact details on Android

I would like my code to update contact details (like name, phone number, email, organization details, etc) in the android contact book. I was successful in modifying a few (name, phone number and email to be specific) but not all.

Whenever I try to update the organization details (Contacts.Organizations.COMPANY and Contacts.Organizations.TITLE) for a contact my app throws an exception

java.lang.UnsupportedOperationException: Cannot update URL: content://contacts/people/69/organizations/69

the code snippet is as follows:

Uri baseUri = ContentUris.withAppendedId(People.CONTENT_URI, 69);
Uri uri = Uri.withAppendedPath(baseUri, People.Phones.CONTENT_DIRECTORY);
Cursor c = this.getContentResolver().query(uri, 
                new String[] { Contacts.Organizations._ID, Contacts.Organizations.COMPANY,Contacts.Organizations.TITLE}, 
                null, null, null);
if(c.getCount() > 0) {
      uri = ContentUris.withAppendedId(uri, c.getString(0));
ContentValues val1 = new ContentValues();
val1.put(Contacts.Organizations.COMPANY, "arw");
val1.put(Contacts.Organizations.TYPE, Contacts.Organizations.TYPE_WORK);
val1.put(Contacts.Organizations.TITLE, "abcdef");
this.getContentResolver().insert(uri, val1);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to update any data in the contacts you need to know the existing id for that fields. Then I used the Builder class to obtain separate ContentProviderOperation objects for the various fields I wanted to update, add them to an arrayList and then use the ContentProvider.applyBatch() method


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...