0

Please help! I cannot figure out why this method is never returning a phone number. I understand that some contacts may not have a phone number, but it is always, always returning null. I am feeding the method a contact Id that I retrieved earlier.

    private String retrieveContactNumber(String id) {


    String contactNumber = "not set, null";

    Log.d(TAG, "Contact ID: " + id);

    Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                    ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                    ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

            new String[]{id},
            null);

    if (cursorPhone.moveToFirst()) {
        contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }

    cursorPhone.close();

    Log.d(TAG, "Contact Phone Number: " + contactNumber);

    return contactNumber;
}
Lee Ames
  • 339
  • 1
  • 3
  • 13
  • Please don't repeat questions. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working will bump it to the top of the active queue. – Mike M. May 02 '17 at 13:38
  • Thank you Mike - I did do that. But didn't get any more responses. I did find a solution however. I am now using DISPLAY_NAME instead of id and the code is finally working. This post gave me a clue: http://stackoverflow.com/questions/8735683/retrieving-a-phone-number-with-contactscontract-in-android-function-doesnt-wo – Lee Ames May 02 '17 at 13:54

0 Answers0