Search code examples
javaandroidandroid-contactsphone-number

Retrieving a phone number with ContactsContract in Android - function doesn't work


I wrote the following function in order to retrieve one single phone number that belongs to the contact with id "contactID".

The function which is to retrieve the phone number:

private String getContactPhone(String contactID) {
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = null;
    String where = ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?";
    String[] selectionArgs = new String[] { contactID };
    String sortOrder = null;
    Cursor result = managedQuery(uri, projection, where, selectionArgs, sortOrder);
    if (result.moveToFirst()) {
        String phone = result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        if (phone == null) {
            result.close();
            return null;
        }
        result.close();
        return phone;
    }
    result.close();
    return null;
}

How this function is called:

ArrayList<Contact> resultContacts = new ArrayList<Contact>();
Cursor result = null;
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Event.CONTACT_ID,
        ContactsContract.CommonDataKinds.Event.START_DATE,
};
String where = ContactsContract.Data.MIMETYPE+" = ? AND "+ContactsContract.CommonDataKinds.Event.TYPE+" = "+ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[] {ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
String sortOrder = null;
result = managedQuery(uri, projection, where, selectionArgs, sortOrder);
while (result.moveToNext()) {
    Long id = result.getLong(result.getColumnIndex(ContactsContract.Contacts._ID));
    String phone = getContactPhone(String.valueOf(id));
    ...
}
...

Unfortunately, it doesn't work. I get null if I call this function with the value that I got from "ContactsContract.Contacts._ID". Why is this so? What is wrong?

Edit: I used to map Contacts._ID to CommonDataKinds.Phone.CONTACT_ID - which didn't work. But now I map Contacts.DISPLAY_NAME to CommonDataKinds.Phone.DISPLAY_NAME and it works suddenly - strange, isn't it? But I would rather like to map the IDs instead of the display names. So the question is still topical. Could this be due to different IDs in those tables? Isn't this why there are lookup IDs?


Solution

  • To get the contact id in the first part, you should use:

    ContactsContract.Data.CONTACT_ID
    

    instead of:

    ContactsContract.Contacts._ID
    

    So the projection should be:

    String[] projection = new String[] {
             ContactsContract.Data.CONTACT_ID,
             ContactsContract.CommonDataKinds.Event.CONTACT_ID,
             ContactsContract.CommonDataKinds.Event.START_DATE,
     };
    

    And then of course get the correct row:

    Long id = result.getLong(result.getColumnIndex(ContactsContract.Data.CONTACT_ID));