-3

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);
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
pankajagarwal
  • 13,462
  • 14
  • 54
  • 65
  • 5
    This isn't a question. Before anyone can help, you need to explain what you think is the problem, and give us more information. – Matt Luongo Feb 04 '10 at 14:43
  • 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 – pankajagarwal Feb 05 '10 at 04:33

3 Answers3

2

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

pankajagarwal
  • 13,462
  • 14
  • 54
  • 65
1
String[] projection = new String[] { ContactsContract.RawContacts._ID };
        String where = ContactsContract.RawContacts.CONTACT_ID + " = ?";
        String[] selection = new String[] { String.valueOf(1) };
        Cursor c = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, where, selection, null);
        c.moveToFirst();
        for(int i=0;i<c.getCount();i++)
        {
            Log.e("id",""+c.getString(0));
            c.moveToNext();
        }



        ContentValues values = new ContentValues();
        values.clear();
        values.put(ContactsContract.RawContacts._ID , 0);
        getContentResolver().update(ContactsContract.RawContacts.CONTENT_URI, values,ContactsContract.RawContacts._ID + "+ ?",new String[] { String.valueOf(1) });
iAndroid
  • 951
  • 7
  • 18
1

I spent a lot of time trying to update the company and title information. The following now works for me.

Uri workUri = Uri.withAppendedPath(contactUri, android.provider.Contacts.Organizations.CONTENT_DIRECTORY);
            values.clear();
            values.put(android.provider.Contacts.Organizations.COMPANY, "company");
            values.put(android.provider.Contacts.Organizations.TYPE, android.provider.Contacts.Organizations.TYPE_WORK);
            values.put(android.provider.Contacts.Organizations.TITLE, "job title");
            values.put(android.provider.Contacts.Organizations.ISPRIMARY, 1);
            getContentResolver().insert(workUri, values); 
Macarse
  • 91,829
  • 44
  • 175
  • 230
michael23
  • 3,746
  • 1
  • 15
  • 10