N Jay
N Jay

Reputation: 1824

retrieve contacts Given_name, family_name, prefix and suffix from Android Contacts

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.

Upvotes: 0

Views: 1144

Answers (1)

Ankur Kumar
Ankur Kumar

Reputation: 972

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);

Upvotes: 0

Related Questions