0

Is there a way to retrieve contacts Given_name family_name, prefix and suffix ? using one query for all contacts.

I found a lot of solutions that suggest querying table ContactsContract.Data.CONTENT_URI but they specify the ID of the contact that they want to retrieve information for. I want to create like a join between the table Contacts and Data to retrieve all contacts but with extra information.

Cœur
  • 37,241
  • 25
  • 195
  • 267
N Jay
  • 1,774
  • 1
  • 17
  • 36

1 Answers1

0

You can get the types by querying this Structured Name Content URI.

http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.StructuredName.html

It has all the attributes you need, just keep the selection empty and you will have structured name data for all the contacts.

Use this code to query the contacts Given name, middle name, family name and prefix

            Uri nameUri = ContactsContract.Data.CONTENT_URI;
        String selection = ContactsContract.Data.MIMETYPE + "='" 
                    + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'";

        Cursor cursor = getContentResolver().query(nameUri, new String[]{
                ContactsContract.CommonDataKinds.StructuredName._ID,
                ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.StructuredName.PREFIX,
                ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,

                }, selection, null, ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
Ankur Kumar
  • 972
  • 7
  • 12
  • can you give details of what you mean exactly ? and an example? – N Jay Nov 16 '14 at 19:52
  • i know that i can query Data table to get all the structured names, but i need to query contacts table and join to data to get all the structured names. – N Jay Nov 16 '14 at 19:56
  • The Data table is already a join of all major contact tables, what exact information do you want so as to require this join? http://developer.android.com/reference/android/provider/ContactsContract.DataColumnsWithJoins.html – Ankur Kumar Nov 17 '14 at 02:26
  • how can you do a query that will retrieve all contacts with the first name, last name and prefix ? – N Jay Nov 17 '14 at 03:45
  • This returns list of all elements inside the Data and not inside the Contacts which results in extra rows being returned that are not actually contacts and thats wats im trying to avoid . – N Jay Nov 17 '14 at 06:16
  • This returns all rows in data view marked with mimetype as Structured name, if you are getting empty rows you can filter them out by changing the selection as String selection = ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + "!=''"; There will be some empty contacts created and marked as deleted by the sync adapters. The data view is already joined with Contacts table as explained earlier. – Ankur Kumar Nov 17 '14 at 09:10