1

I want the Name of the Contact which includes (FAMILY_NAME, GIVEN_NAME,MIDDLE_NAME,PHONETIC_FAMILY_NAME,PHONETIC_GIVEN_NAME,PHONETIC_MIDDLE_NAME,PREFIX,SUFFIX).

I know that column names of the above data that starts with

android.provider.ContactsContract.CommonDataKinds.StructuredName

But i am unable to get the URI of the data.

I was working on device with api level 8 so i want to fetch these details using

android.provider.ContactsContract

I have searched about this in commumnity but i can't get the desired result.

I am working for 4 hours on this.

Any help will be appreciated.

I was using this code

 Cursor cursor = activity.managedQuery(android.provider.ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cursor != null) 
        {
            if (cursor.moveToFirst()) 
            {

                int rows = cursor.getCount();
                int cols = cursor.getColumnCount();

                 do 
                    {

                         int _id = cursor.getInt(cursor.getColumnIndex("_id"));

                         int times_contacted = cursor.getInt(cursor.getColumnIndex("times_contacted"));
                         int has_phone_number = cursor.getInt(cursor.getColumnIndex("has_phone_number"));
                         int send_to_voicemail = cursor.getInt(cursor.getColumnIndex("send_to_voicemail"));
                         int starred = cursor.getInt(cursor.getColumnIndex("starred"));

// Here i want the contact names But i dont know what column name i have to pass to get them                      

                        }
                    while (cursor.moveToNext());    
            }
            cursor.close();
        }

Thanks in advance.

KK_07k11A0585
  • 2,381
  • 3
  • 26
  • 33

1 Answers1

2

Ok, Try this, and let me know what happen,

Look at ContactsContract.CommonDataKinds.StructuredName class. You can find there all columns you are looking for.

   String whereName = ContactsContract.Data.MIMETYPE + " = ?";
    String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
    Cursor nameCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
    while (nameCur.moveToNext()) {
        String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
        String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
        String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
    }
    nameCur.close();

Look at this SO question How to get the firstname and lastname from android contacts?

EDIT: Look at this complete example for working with android contacts Working With Android Contacts, Now if you want to get more info from any contacts then add a particular column on that cursor. For more columns look at ContactsContract.CommonDataKinds.

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
  • hi **user370305** How to get all contact numbers of a particular contact like Mobile number,Work number, Home number, Other numbers. Do u have any idea ????????? – KK_07k11A0585 Dec 02 '11 at 08:35
  • [ContactsContract.CommonDataKinds.Phone](http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html) its for column name of different types of phone numbers.. – user370305 Dec 02 '11 at 09:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5522/discussion-between-kk-07k11a0585-and-user370305) – KK_07k11A0585 Dec 02 '11 at 09:33
  • @user370305 u have nice answer here.i want to fetch email id of a particular number.can i do that in android ? – Android Killer May 16 '13 at 05:55
  • @AndroidKiller - Yes, For that you have to query on `ContactsContract.CommonDataKinds.Email` Uri. Look at http://stackoverflow.com/questions/10117049/get-only-email-address-from-contact-list-android – user370305 May 16 '13 at 17:56
  • @user370305 already tried that link.no use throwing exception. – Android Killer May 16 '13 at 17:59
  • @AndroidKiller - What kind of Exception? – user370305 May 16 '13 at 18:02