title: Retrieving a phone number with ContactsContract in Android - function doesn't work
body: <p>I wrote the following function in order to retrieve one single phone number that belongs to the contact with id "contactID".</p>
  
  <p><strong>The function which is to retrieve the phone number:</strong></p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p><strong>How this function is called:</strong></p>
  
  <pre><code>ArrayList&lt;Contact&gt; resultContacts = new ArrayList&lt;Contact&gt;();
  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));
      ...
  }
  ...
  </code></pre>
  
  <p>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?</p>
  
  <p><strong>Edit:</strong> 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?</p>