Search code examples
androidandroid-contentproviderandroid-contacts

Update contact name in android 4.4


I would like to modify contact's name in an Android device (GT N5110) with Android 4.4. I have tried to do that by this approach:

ContentValues contentValues = new ContentValues();
String selection = ContactsContract.Contacts._ID + " = ? " ;
String[] selectionArgs = new String[] { Integer.toString(id) };
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, lastName);
contentValues.put(ContactsContract.Contacts.STARRED, 2); // *
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName);
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName);
return this.context.getContentResolver().update(ContactsContract.Contacts.CONTENT_URI,
            contentValues, selection, selectionArgs);

The line marked with * was the only one which modified the contact database. I also tried by applyBatch and added the MIMETYPE selection (ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE). The MIMETYPE approach throws some Exception with these message:

android.database.sqlite.SQLiteException: no such column: mimetype (code 1): , while compiling: SELECT _id FROM view_contacts_restricted WHERE _id = ? AND mimetype=?

I researched and tried suggestions present in update contacts display_name and Modifying contact information.

Can someone help me?


Solution

  • The following website explains it to you. It is a full tutorial by android itself. I highly recommend you use this website to first check if there is an answer to your query first before coming here.

    http://developer.android.com/reference/android/provider/ContactsContract.Intents.html#EXTRA_FORCE_CREATE

    What this website explains to you is Send data to the contact you require to edit and it automatically opens the contact edit screen with the details which you passed to it. If you want to know what all extras you can send to it. Visit the following link. https://developer.android.com/reference/android/provider/ContactsContract.Intents.Insert.html

    I am not sure if this is correct but it may be because of some security policy or so that you can't skip ui confirmation. But again I am not familiar with this so I may be wrong. I have not tested if this works or anything but I think it might help. Especially the second one:

    http://developer.android.com/reference/android/provider/ContactsContract.Intents.html#EXTRA_FORCE_CREATE

    And

    http://www.techjini.com/blog/2011/10/31/insert-and-modify-contact-in-android/