0

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?

Community
  • 1
  • 1
Everton
  • 5
  • 3

1 Answers1

0

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/

KISHORE_ZE
  • 1,466
  • 3
  • 16
  • 26
  • The suggestion you have made opens some contact Activity for update. I need to do that by programming, with no user interference. These update will occurr in a Service. – Everton Aug 22 '15 at 12:29
  • Is there some way to avoid contact screen to update display_name contact? – Everton Aug 22 '15 at 13:21
  • I don't think so but I'll keep looking – KISHORE_ZE Aug 22 '15 at 16:22
  • The approach pointed out in http://developer.android.com/reference/android/provider/ContactsContract.Intents.html#EXTRA_FORCE_CREATE will open the contact activity. These behaviour is undesirable, because i'm trying to modify contact name with no user interference. The contact update should happen in background. – Everton Aug 22 '15 at 22:31
  • As I said this could be because of a security policy – KISHORE_ZE Aug 23 '15 at 05:00
  • Also did you see the 2nd one. I mentioned it has a high chance of working – KISHORE_ZE Aug 23 '15 at 05:01