I wrote the following function to get one phone number that belongs to a contact with the identifier "contactID".
Function that is designed to retrieve a 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 does not work. I get null if I call this function with a value that I got from "ContactsContract.Contacts._ID". Why is this so? What's wrong?
Edit: I used the Contacts._ID card for CommonDataKinds.Phone.CONTACT_ID - this did not work. But now I map Contacts.DISPLAY_NAME to CommonDataKinds.Phone.DISPLAY_NAME, and it works all of a sudden - it's strange, isn't it? But I would prefer matching identifiers instead of display names. Therefore, the issue remains relevant. Could this be due to different identifiers in these tables? Doesn't that mean there are search identifiers?
source share