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

后端 未结 3 1115
遇见更好的自我
遇见更好的自我 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);
    }
    
    0 讨论(0)
  • 2020-12-16 22:35
            Cursor phone_cursor = cr.query(ContactsContract.CommonDataKinds.
                    Phone.CONTENT_URI, null, null, null, null);
            while (phone_cursor.moveToNext()) {
                try {
                int id = Integer.parseInt(phone_cursor.getString(phone_cursor.getColumnIndex
                        (ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
                Cursor name_cursor = cr.query(ContactsContract.Data.CONTENT_URI,null,
                        ContactsContract.Data.CONTACT_ID + "  = " + id, null, null);
    
                    String name = phone_cursor.getString(phone_cursor.getColumnIndex
                            (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String first_name ="";
                    String last_name = "";
                    while (name_cursor.moveToNext()) {
                        if(name_cursor.getString(name_cursor.getColumnIndex
                                (ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))!=null){
                        first_name = name_cursor.getString(name_cursor.getColumnIndex
                                (ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                        last_name = name_cursor.getString(name_cursor.getColumnIndex
                                (ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                    }}
                    name_cursor.close();
                    String phoneNumber = phone_cursor.getString(phone_cursor.getColumnIndex
                            (ContactsContract.CommonDataKinds.Phone.NUMBER));
                } catch (Exception e) {
                }
            }
            phone_cursor.close();
    
    0 讨论(0)
  • 2020-12-16 22:42

    Here is a general function for getting user data from ContactsContract.Data table:

    Map<String, String> result = new HashMap<>();
    Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.CONTACT_ID + "='" + YOUR_CONTACT_ID + "'", null, null);
    if (cursor != null) {
        while (cursor.moveToNext()) {
            String mime = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
            switch (mime) {
                case ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE:
                    result.put(FIRST_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)));
                    result.put(LAST_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)));
                    break;
                case ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE:
                    result.put(CITY, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)));
                    result.put(STREET, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
                    result.put(ZIP, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)));
                    break;
                case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE:
                    if (ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE == cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))) {
                        result.put(MOBILE, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                    }
                    break;
            }
        }
        cursor.close();
    }
    return result;
    
    0 讨论(0)
提交回复
热议问题