-1

I know this is a common question since I found many answers about it, but I still have an issue regarding phone number retrieving.

I want to get ALL numbers from one contact picked from the PICK_CONTACT activity.

Here is my code from the onActivityResult() block

 @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) {

                ArrayList<String> allContacts = new ArrayList<String>();

                Uri contactData = data.getData();
                Cursor c =  getContentResolver().query(contactData, null, null, null, null);
                while (c.moveToNext()) {

                    String contact_id = c.getString(c.getColumnIndex( _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 + " = ?", new String[] { contact_id }, null);
                        while (phones.moveToNext())
                        {
                            String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            allContacts.add(contactNumber);

                            break;
                        }
                        phones.close();

                    }
                    String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    TXT_EmergencyContactNameProfile.setText(name);
                }

            }
            break;
    }
}

I expected the loop while (phones.moveToNext()) to get all different numbers ... but no

Thank you very much in advance ;)

GrayFox
  • 824
  • 2
  • 10
  • 27
  • You wrote what you don't get, not what you get :) What's the result you get now? – MorZa Aug 17 '16 at 11:39
  • Hey ! First, thanks ! I only get the first phone number from the picked contact. I would like to get them all :) – GrayFox Aug 17 '16 at 12:24
  • First of all I think I would try to print to logcat the phone number in the while loop to check how many times it goes inside the loop (of course after making sure the selected contact does have more than one phone number). Maybe you should delete the "break;" – MorZa Aug 17 '16 at 12:36
  • Yep I did that, I also put a breakpoint and the loop is raised only once. Removing the break statement may be a good idea :p I keep you in touch – GrayFox Aug 17 '16 at 12:54
  • OMG I'm so stupid that's it ! Thank you MorZa, I don't even know why did I put it there .... – GrayFox Aug 17 '16 at 12:57
  • Great, I'm glad it helped you :) It would be great if you can mark it as the correct answer. – MorZa Aug 17 '16 at 12:59

1 Answers1

1

Try to delete the "break;" inside the while loop.

MorZa
  • 2,215
  • 18
  • 33