-4

Edit: ANSWERED I'm using the following code to retrieve a contact phone number from contact list

Intent GetConactIntent = new Intent(Intent.ACTION_PICK, 
                         ContactsContract.Contacts.CONTENT_URI);
                 startActivityForResult(GetConactIntent, PICK_CONTACT);

@Override 
public void onActivityResult(int reqCode, int resultCode, Intent data){ 
    super.onActivityResult(reqCode, resultCode, data);

switch(reqCode)
{
   case (PICK_CONTACT):
     if (resultCode == Activity.RESULT_OK)
     {
         Uri contactData = data.getData();
         Cursor c = managedQuery(contactData, null, null, null, null);
          if (c.moveToFirst())
          {
              String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

              String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

              if (hasPhone.equalsIgnoreCase("1")) 
              {
                  Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                  phones.moveToFirst();
                  //PhoneNumber
                  String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                   Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_SHORT).show();

                  //ContactName
                   String nameContact = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                  TextView txtNum =(TextView)VodafoneActivity.this.findViewById(R.id.editTextNum);

                  //Execute
                  txtNum.setText( cNumber);
              }
         }
   }
}

}

But this code only views the contact name and when I select a contact it selects the first number in the contact details, however in certain cases the contact has more than number and I want to give the user the ability to choose from numbers.
Any ideas?

I need something like this:

Image Description for what I need

2 Answers2

0

try this if this code doesn't help

Cursor cursor = null;
try {
    cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
    int contactIdIdx = cursor.getColumnIndex(Phone._ID);
    int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
    int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
    int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
    cursor.moveToFirst();
    do {
        String idContact = cursor.getString(contactIdIdx);
        String name = cursor.getString(nameIdx);
        String phoneNumber = cursor.getString(phoneNumberIdx);
        //...
    } while (cursor.moveToNext());  
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (cursor != null) {
        cursor.close();
    }
}
Community
  • 1
  • 1
Jemo Mgebrishvili
  • 5,187
  • 7
  • 38
  • 63
  • 1
    What does this code? How does it work? What is Cursor? Why does this answer the question of the OP? – rene Jan 14 '16 at 09:08
  • Retrieving contact from contact list – Jemo Mgebrishvili Jan 14 '16 at 09:14
  • i think the edit will be in the first part of the code when you call the contacts list – Sary Elgmal Jan 14 '16 at 09:22
  • I didn't leave my initial comment to get a comment as response. I think the quality of your answer can be improved as explained [here](http://meta.stackexchange.com/questions/148272/is-there-any-benefit-to-allowing-code-only-answers-while-blocking-code-only-ques) and [here](http://meta.stackoverflow.com/questions/256359/flag-try-this-code-answers-as-very-low-quality). Notice how the consensus is to down-vote answers that are nothing more then *try this {code block}* – rene Jan 14 '16 at 09:30
0

found it Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI.

Intent intent = new Intent(Intent.ACTION_PICK, CommonDataKinds.Phone.CONTENT_URI);