title: Retrieving phone number from contact id : android
body: <p>I'm trying to get contact data once a contact name has been clicked from my list view. As per the code below, I can log successfully the ID of my contact, but haven't managed to use the ContactsContract to retrieve the data. What's the best way to do this? (have tried <a href="https://stackoverflow.com/questions/3370628/retrieve-contact-phone-number-from-uri-in-android">Retrieve Contact Phone Number From URI in Android</a> to not much avail)</p>
  
  <p>EDIT 2 : Fixed code, now works</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState)
      {
          Log.v(TAG, "Activity State: onCreate()");
          super.onCreate(savedInstanceState);
          setContentView(R.layout.contact_manager);
          mContactList = (ListView) findViewById(R.id.contactList);
          populateContactList();
  
          mContactList.setOnItemClickListener(new OnItemClickListener() {
              String strid = Long.toString(id);               
              Cursor result = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID +" = ?", new String[]{strid}, null);
              if (result.moveToFirst()) { 
                   Cursor c = getContentResolver().query(Data.CONTENT_URI,
                            new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
                            Data.RAW_CONTACT_ID + "=?" + " AND "
                                    + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
                            new String[] {String.valueOf(strid)}, null);
                   if(c.moveToFirst()){
                       int phoneColumn = c.getColumnIndex("data1");
                       String phoneNumber = c.getString(phoneColumn);
                       Log.d("DATA",phoneNumber);
                   }
              }
          });
      }
  </code></pre>
  
  <p>EDIT 1 : forgot some important stuff. The code is adapted from the ContactManager example from the Android dev site.</p>
  
  <pre><code>/**
   * Populate the contact list based on account currently selected in the account spinner.
   */
  private void populateContactList() {
      // Build adapter with contact entries
      Cursor cursor = getContacts();
      String[] fields = new String[] {
              ContactsContract.Data.DISPLAY_NAME
      };
      SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
              fields, new int[] {R.id.contactEntryText});
      mContactList.setAdapter(adapter);
  }
  
  /**
   * Obtains the contact list for the currently selected account.
   *
   * @return A cursor for for accessing the contact list.
   */
  private Cursor getContacts()
  {
      // Run query
      Uri uri = ContactsContract.Contacts.CONTENT_URI;
      String[] projection = new String[] {
              ContactsContract.Contacts._ID,
              ContactsContract.Contacts.DISPLAY_NAME
      };
      String selection = null;
      String[] selectionArgs = null;
      String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
  
      return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
  }
  </code></pre>