0

I try to sync android contacts. I want to use first and family name to identify a contact and update it with the raw_id, but I don't know how to search for family name and surname. I looked at the sample files, but there's only a description to search (and sync) with the raw_id!

fillibuster
  • 698
  • 4
  • 16
  • 33

2 Answers2

1

The "family" name as a separate field isn't part of the Contacts table.

Rows in the Contacts table are generated automatically by the provider. In general, one Contacts row aggregates one or more raw contacts in ContactsContract.RawContacts. Each raw contact has a StructuredName row that contains the family name.

To do the search you describe, search ContactsContract.CommonDataKinds.StructuredName on FAMILY_NAME and GIVEN_NAME. Provide a projection that includes LOOKUP_KEY, which is a "permanent" link to the row in ContactsContract.Contacts that you want.

Alas, this is based only on the documentation. LOOKUP_KEY is supposed to be part of the columns available to a query on StructuredName, but I've seen other cases where the documentation didn't match the implementation. Apparently, the developers sometimes include a list of implemented column names, but don't actually seem to implement returning them.

Also, the original response used getContentResolver(). Please use CursorLoader instead, whenever possible.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
  • Hi,I understand not everthing you wrote (new to Android). I've done this: Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + "= ?", new String[]{"testuser"}, null);. cursor.getCount() returns 1 which is correct, but how to get a unique _id to update the whole contact? – fillibuster Jan 15 '13 at 20:24
  • @grolle Check this SO question [link](http://stackoverflow.com/questions/4301064/how-to-get-the-first-name-and-last-name-from-android-contacts?rq=1) – sinisha Jan 15 '13 at 21:20
  • Hi, ok I've done it: I use a projection to return the Lookup_key. One last question left: Is the Lookup_key the unique key for updating an contact or only the key to get the contact id which is the right one for an update? – fillibuster Jan 15 '13 at 21:26
0

Just use the Query command's selection argument.

Cursor cursor = Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, ContactsContract.Contacts.DISPLAY_NAME + "=" + familyName, null, null); 

And then loop through your Cursor and look for the id:

String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 

You also could create a whole DataSource class and make the call easier.

hnzlmnn
  • 393
  • 2
  • 14
  • 1
    Hi, ok I give it a try. One question to your source: did I search with ContactsContract.Contacts.DISPLAY_NAME the family name? I think the Display name could be different, or not? – fillibuster Jan 15 '13 at 19:15
  • 2
    DISPLAY_NAME is not the same as the FAMILY_NAME that's stored. DISPLAY_NAME contains a full name; if you look at a list of people in the People app, you see their display name. I am curious; why do you want to search yourself, instead of using an Intent with ACTION_PICK to allow the user to pick names from the People app? – Joe Malin Jan 15 '13 at 20:30
  • Hi Joe, I want to sync a list (csv) with the contacts. If user (FAMILY_NAME+GIVEN_NAME) exists update else insert! – fillibuster Jan 15 '13 at 20:34
  • Hey grolle, the Constant DISPLAY_NAME was just an example. You just have to change it to the value you want.. – hnzlmnn Jan 25 '13 at 09:32