2

A contact may have many phone numbers (mobile, home, ..). I want to enable the user to pick one of the phone numbers of a specific contact.

With this snippet I get the list of all phone numbers for each contact.

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

How can I list only the phone numbers of one contact?

Edit: I know how to get all phone numbers of a contact, that's not the point. I could put all phone numbers in a listview and get the user to pick one. But this functionality exists (mentioned above), I just don't want all numbers, but only the phone numbers for one contact.

Juri Glass
  • 88,173
  • 8
  • 33
  • 46
  • please be specific in your question. In your "edit" you are contradicting yourself. Can you tell me what is the diff btw "all phone numbers of a contact" and "the phone numbers for one contact"? – mudit Apr 05 '11 at 08:50
  • Sorry, if that is not clear. Both are the same for me. I want to filter the list, given by the ContactsContract.CommonDataKinds.Phone.CONTENT_URI, so that the displayed list consists only of phone numbers attached to a given contact. – Juri Glass Apr 05 '11 at 09:36

2 Answers2

3

IF you want to get all the phone numbers related to a contact then:

1) Use this intent to open contacts app:

 Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    startActivityForResult(intent, PICK_CONTACT);

2) in onActivityResult use following code:

if (requestCode == PICK_CONTACT) {
            if (resultCode == Activity.RESULT_OK) {
                if (data != null) {
                    Uri contactData = data.getData();

                    try {

                        String id = contactData.getLastPathSegment();
                        Cursor phoneCur = getContentResolver()
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = ?", new String[] { id },
                                        null);

                        final ArrayList<String> phonesList = new ArrayList<String>();
                        while (phoneCur.moveToNext()) {
                            // This would allow you get several phone addresses
                            // if the phone addresses were stored in an array
                            String phone = phoneCur
                                    .getString(phoneCur
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
                            phonesList.add(phone);
                        }
                        phoneCur.close();

                        if (phonesList.size() == 0) {
                            Helper.showToast(
                                    this,
                                    getString(R.string.error_no_phone_no_in_contact),
                                    Toast.LENGTH_LONG);
                        } else if (phonesList.size() == 1) {
                            editText.setText(phonesList.get(0));
                        } else {

                            final String[] phonesArr = new String[phonesList
                                    .size()];
                            for (int i = 0; i < phonesList.size(); i++) {
                                phonesArr[i] = phonesList.get(i);
                            }

                            AlertDialog.Builder dialog = new AlertDialog.Builder(
                                    SendSMS.this);
                            dialog.setTitle(R.string.choose_phone);
                            ((Builder) dialog).setItems(phonesArr,
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            String selectedEmail = phonesArr[which];
                                            editText.setText(selectedEmail);
                                        }
                                    }).create();
                            dialog.show();
                        }
                    } catch (Exception e) {
                        Log.e("FILES", "Failed to get phone data", e);
                    }
                }
            }
        }

This will set the selected phone no in a edit text named editText. You can change this as per your need.

mudit
  • 25,306
  • 32
  • 90
  • 132
  • I like the simple use of the alert dialog for more than 1 in the result :-) – Paul Kohler Apr 30 '13 at 05:31
  • I modified your code to also get the name and contact pic but it's not working. Any way to assist me? Also any way to modify to display the type of number it is next to it? – Si8 Oct 10 '13 at 03:17
  • Would you mind taking a look at this for me? http://stackoverflow.com/questions/19286637/phone-numbers-along-with-name – Si8 Oct 10 '13 at 03:38
0

Take a look at these two pages:

  1. Read all contact's phone numbers in android

  2. http://developer.android.com/resources/articles/contacts.html

They should make it clear how to get the data you want.

Are you also stuck on the display of the data?

Community
  • 1
  • 1
Thane Anthem
  • 4,093
  • 4
  • 26
  • 24