Skip to content

Instantly share code, notes, and snippets.

@chemouna
Last active November 4, 2015 11:30
Show Gist options
  • Save chemouna/b10f885eab3f9756a629 to your computer and use it in GitHub Desktop.
Save chemouna/b10f885eab3f9756a629 to your computer and use it in GitHub Desktop.
It's pretty hard to find the exact query to do to get a contact's name , So here's how :
String[] nameProjection = new String[] {
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME
};
final Cursor nameCursor = context.getContentResolver()
.query(ContactsContract.Data.CONTENT_URI, nameProjection,
ContactsContract.Data.LOOKUP_KEY + " = ? AND "+ ContactsContract.Data.MIMETYPE + " = ? ",
new String[] { contactLookupKey, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE },
null, null);
try {
if (nameCursor.moveToNext()) {
String firstName = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
if(!TextUtils.isEmpty(firstName)) {
displayName = firstName;
}
}
} finally {
nameCursor.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment