Last active
November 4, 2015 11:30
-
-
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 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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