Reputation: 698
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!
Upvotes: 0
Views: 2059
Reputation: 8641
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.
Upvotes: 1
Reputation: 393
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.
Upvotes: 0