Get the first and last name of the contact, not one display name?

I am currently working with the Android Contacts content provider and currently cannot access the full-featured display name of the contacts using the following code:

String[] PROJECTION = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER, }; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; String SELECTION = "LOWER(" + ContactsContract.Contacts.DISPLAY_NAME + ")" + " LIKE '" + constraint + "%' " + "and " + ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'"; Cursor cur = managedQuery(ContactsContract.Contacts.CONTENT_URI, PROJECTION, SELECTION, null, sortOrder); 

However, I want to get both the contacts and the last name separately, I tried to use StructuredName in an attempt to get this, but I can not get it to work.

Can someone point me in the right direction how to use StructuredName correctly to split the name into First and Last?

UPDATE:

Following hovanessyan's advice, I tried to do the following:

  String[] PROJECTION = new String[] { ContactsContract.Data._ID, ContactsContract.Data.DISPLAY_NAME, ContactsContract.Data.HAS_PHONE_NUMBER, }; String SELECTION = ContactsContract.CommonDataKinds.StructuredName.IN_VISIBLE_GROUP + " = '1'"; String sortOrder = ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, sortOrder); 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); Log.e("XXX", "Name: | " + given + " | " + family + " | " + display); } 

However, using PROJECTION fails as follows:

 12-08 16:52:01.575: E/AndroidRuntime(7912): Caused by: java.lang.IllegalArgumentException: Invalid column has_phone_number 

If I delete PROJECTION, I get all the printed results, but many of them contain NULL.

For example:

 12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null 12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null 12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null 12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null 

So can anyone see what I am doing wrong in that my PROJECTION is not working?

FURTHER UPDATE:

I found out the problems with my PROJECTION, but now I have a problem when the DATA content provider returns me all null data and throws null pointers in my code.

The number of cursors from ContactsContract.Contacts returns me 115, for example, but using the DATA table returns 464 to me using the same parameters, and this causes huge problems in my application.

Does anyone have any idea why this is?

+8
android cursor contactscontract android-contacts
Dec 08 2018-11-12T00:
source share
3 answers

See the ContactsContract.CommonDataKinds.StructuredName class. You have all the necessary columns, and you can 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); } 
+13
Dec 08 '11 at 1:55 a.m.
source share

Here is a common function to get user data from the 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; 
+2
Mar 24 '14 at 11:42
source share
  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(); 
+2
Oct 29 '14 at 11:17
source share



All Articles