Get first and last name of a contact rather than single display name?

后端 未结 3 1120
遇见更好的自我
遇见更好的自我 2020-12-16 22:10

I am currently working with the Android Contacts content provider and currently can access a contacts full display name without issue using the following code:



        
3条回答
  •  臣服心动
    2020-12-16 22:32

    Take a look at ContactsContract.CommonDataKinds.StructuredName class. You have all the needed columns there, and you can probably do something like:

    Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, other_query_params_for_filtering);
    
    int indexGivenName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
    int indexFamilyName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
    int indexDisplayName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
    
    while (cursor.moveToNext()) {
        String given = cursor.getString(indexGivenName);
        String family = cursor.getString(indexFamilyName);
        String display = cursor.getString(indexDisplayName);
    }
    

提交回复
热议问题