4

I am retrieving firstname and lastname from the android contact using the below code.DISPLAY_NAME gives back the name of the contact while firstname and lastname returns 1 and null respectively.The following is the code.

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
if (cur.getCount() > 0) {
    while (cur.moveToNext()) {
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String firstname = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                    String lastname = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                    System.out.println("firstname and lastname" + firstname+ " "+lastname);
                    phoneNo = phoneNo.replaceAll("\\D", "");
                    names.add(name);
                    phnno.add(phoneNo);
                }
                pCur.close();
            }
        }           
    }

When i change the line cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI to cr.query(ContactsContract.Data.CONTENT_URI i get the log as

enter image description here

Suggestions are Highly appreciated.Thanks in advance

hemanth kumar
  • 3,068
  • 10
  • 41
  • 64

1 Answers1

10

The following code will help you get first name and last name:

Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri dataUri = Uri.withAppendedPath(contactUri, Contacts.Data.CONTENT_DIRECTORY);
Cursor nameCursor = getActivity().getContentResolver().query(
        dataUri,
        null,
        Data.MIMETYPE+"=?",
        new String[]{ StructuredName.CONTENT_ITEM_TYPE },
        null);
           while (nameCursor.moveToNext())
            {

                String      firstName = nameCursor.getString(nameCursor.getColumnIndex(Data.DATA2));
                String  lastName = nameCursor.getString(nameCursor.getColumnIndex(Data.DATA3));

                Toast.makeText(getApplicationContext(), "First name"+firstName, Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "Second name"+lastName, Toast.LENGTH_LONG).show();

                return new String [] {firstName , lastName};
            }

           nameCursor.close();
Joyson
  • 1,643
  • 5
  • 28
  • 48
  • Thanks, after tons of searching, this is the only solution that worked for me. It's too bad that the constants have names like "DATA1" and "DATA2" instead of something more intuitive or meaningful. – CACuzcatlan Jun 05 '13 at 21:26
  • 4
    You should be using this instead of DATA1, DATA2 etc. http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.StructuredName.html – Dennis K Sep 18 '13 at 20:29
  • Joyson, Thank you. It's really helpful. Only this code work for me – Konstantin Apr 19 '16 at 11:28