0

In Android, when the user selects one contact using the contact picker, you get only the CONTACT_ID field. But for updating some contact info, you need to edit the raw contact data. To do so, you have to know the RAW_CONTACT_ID. Is this correct so far?

So how do you get the RAW_CONTACT_ID that you need for updating data when you have only the CONTACT_ID? Like this ... ?

String[] projection = new String[] { ContactsContract.RawContacts._ID };
String where = ContactsContract.RawContacts.CONTACT_ID + " = ?";
String[] selection = new String[] { String.valueOf(contactId) };
Cursor c = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, where, selection, null);

But this way you will get all the RAW_CONTACT_IDs belonging to the picked user, right? Which one is then the raw contact that I should update?

If you want to say "It's up to you": It's all the same to me. I just want to put my data anywhere, in any raw contact that can hold the data.

caw
  • 30,999
  • 61
  • 181
  • 291

2 Answers2

0

You should go through all Raw Contacts and find the best one to update. You can filter out Read-Only contacts like Facebook (RAW_CONTACT_IS_READ_ONLY). Probably you want to look for the Raw Contact that already contains the field to update. If none such exists go for a Google account if present otherwise any writable account. Depending on the situation consider writing to all writable ones.

mattlaabs
  • 486
  • 3
  • 14
0

There's no perfect answer. But the easiest, and probably, most correct is to use the google account.

Which you can do by selecting on the ACCOUNT_TYPE which will be "com.google" for the google account. As there can be many google accounts on the phone just choose the first one (or the one with most contacts).

lordl
  • 471
  • 4
  • 3
  • Thank you! Is there always a Google account? Or can it happen that the query will not find any Google raw contact? I guess there's not always one, right? – caw Jan 30 '12 at 22:39
  • There won't always be one, no. But assuming the app you're developing is downloaded from the android market, you can be fairly sure there will be one. (As you must log in to use the market) – lordl Jan 30 '12 at 22:42
  • But when you sync your contacts with Facebook, e.g., and there's no other raw contact to merge a Facebook contact with, then you will probably have a contact that consists only of the Facebook raw contact whereas it doesn't have any Google raw contact. Isn't this so? – caw Jan 30 '12 at 23:37