1

I do have contact IDs available with me. Is there a way that I can get all the details of the contact (with the help of the contact ID)?

There was already a question exist on Stack Over flow regarding the same but the answer seems to be outdated and doesn't work anymore.

Piyush Malaviya
  • 1,091
  • 13
  • 17
Sanjay Joshi
  • 2,036
  • 6
  • 33
  • 48

1 Answers1

1

Code snippet for fetching all details of contact using contact id.

public static ModelContact getContactDetails(final Context context, String contactId) {

        ModelContact contact = new ModelContact();
        contact.setContactId(contactId);

        Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                new String[]{ContactsContract.Data._ID,
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.Data.DISPLAY_NAME,
                        ContactsContract.Data.RAW_CONTACT_ID,
                        ContactsContract.Data.LOOKUP_KEY,
                        ContactsContract.Data.DATA1,
                        ContactsContract.Data.DATA2,
                        ContactsContract.Data.DATA3,
                        ContactsContract.Data.DATA4,
                        ContactsContract.Data.DATA5,
                        ContactsContract.Data.DATA6,
                        ContactsContract.Data.DATA7,
                        ContactsContract.Data.DATA8,
                        ContactsContract.Data.DATA10},

                ContactsContract.Data.CONTACT_ID + "=?" + " AND "
                        + "(" + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' OR "
                        + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "' OR "
                        + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "' OR "
                        + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "' OR "
                        + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE + "')",
                new String[]{contactId}, null);

        if (cursor != null && cursor.getCount() > 0) {

            while (cursor.moveToNext()) {
                String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                contact.setDisplayName(displayName);
                String rowContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
                contact.setRawContactId(rowContactId);
                String lookUpKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.LOOKUP_KEY));
                contact.setLookupKey(lookUpKey);

                String mimeType = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
                if (mimeType.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
                    setPhoneList(cursor, contact);
                } else if (mimeType.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
                    setEmailList(cursor, contact);
                } else if (mimeType.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
                    setStructuredName(cursor, contact);
                } else if (mimeType.equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
                    setOrganization(cursor, contact);
                } else if (mimeType.equals(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
                    setAddress(cursor, contact);
                }
            }

            cursor.close();
        }
        return contact;
    }

For more details check the following link.

ContactHelper.java

I hope it will help you.

Piyush Malaviya
  • 1,091
  • 13
  • 17
  • Looking into it. Thanks – Sanjay Joshi Dec 17 '17 at 16:08
  • One question buddy, how do you access label and value of getPhoneList()? When I'm trying to get the label like result.get(indexValue).label it work fine in debugger but android studio trowing error at the time of compilation. – Sanjay Joshi Dec 17 '17 at 17:54
  • @SanjayJoshi Did you mean get a label from a ModelContact object.getPhoneList() method? and Can you post the error which you get compilation time? – Piyush Malaviya Dec 17 '17 at 18:38
  • @PiyushMalaviya: Can you help me with getting primary number for a contact? When I try to execute "cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.IS_PRIMARY);" in your "setPhoneList" method, it always return "-1" for all numbers (even for the primary number). Is there a way to know the primary number from existing cursor (in setPhoneList method itself)? – Sanjay Joshi Dec 24 '17 at 16:30