0

I'm using a library from GitHub MultiContactPicker, used to pick contacts using custom ListView. I am able to get the names of contacts picked by the user (onActivityResult) but can't get the phone numbers.

The library uses private List<PhoneNumber> mPhoneNumbers = new ArrayList<>(); And I tried to get this list but the onActivityResult does not return this List. When ContactPicker is called:

new MultiContactPicker.Builder(MainActivity.this) //Activity/fragment context
        .theme(R.style.MyCustomPickerTheme) //Optional - default: MultiContactPicker.Azure
        .hideScrollbar(false) //Optional - default: false
        .showTrack(true) //Optional - default: true
        .searchIconColor(Color.WHITE) //Option - default: White
        .setChoiceMode(MultiContactPicker.CHOICE_MODE_MULTIPLE) //Optional - default: CHOICE_MODE_MULTIPLE
        .handleColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)) //Optional - default: Azure Blue
        .bubbleColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)) //Optional - default: Azure Blue
        .bubbleTextColor(Color.WHITE) //Optional - default: White
        .setTitleText("Select Contacts") //Optional - default: Select Contacts
        .setSelectedContacts("10", "5" / myList) //Optional - will pre-select contacts of your choice. String... or List<ContactResult>
        .setLoadingType(MultiContactPicker.LOAD_ASYNC) //Optional - default LOAD_ASYNC (wait till all loaded vs stream results)
        .limitToColumn(LimitColumn.NONE) //Optional - default NONE (Include phone + email, limiting to one can improve loading time)
        .setActivityAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
                         android.R.anim.fade_in,
                         android.R.anim.fade_out) //Optional - default: No animation overrides
               .showPickerForResult(CONTACT_PICKER_REQUEST);

onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == CONTACT_PICKER_REQUEST){
        if(resultCode == RESULT_OK) {
            List<ContactResult> results = MultiContactPicker.obtainResult(data);
            Log.d("MyTag", results.get(0).getDisplayName());
        } else if(resultCode == RESULT_CANCELED){
            System.out.println("User closed the picker without selecting items.");
        }
    }
}

The onActivityResult; List<ContactResult> results by default gives Names only, But I want to get Phone Numbers also.

Zubair Younas
  • 71
  • 1
  • 13

3 Answers3

1

I find out this repository of github for you AndroidContacts.

Basic usage of this Repo is:

  • Get all contacts from android device
  • Get specific data from contacts
  • Querying inside contacts
  • Save new contacts

OR

Also you can get the phone number using this code by ContentResolver:

final ContentResolver cr = getContentResolver();
String[] projection = new String[] {Contacts.DISPLAY_NAME, Phone.NUMBER};
final Cursor c = cr.query(Data.CONTENT_URI, projection, null, null, null);
myCursorAdapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[] {Phone.NUMBER}, new int[]{R.id.TVRow}, 0);
myPhoneList.setAdapter(myCursorAdapter);

myPhoneList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        c.moveToPosition(position);
        Toast.makeText(getApplicationContext(), c.getString(1), Toast.LENGTH_SHORT).show();
    }
});

For better understanding go to official documentation

Ans reference: if you want to here is the answer already given

Hamad
  • 5,096
  • 13
  • 37
  • 65
  • The OP is asking for a particular library @hamad – Deepak Kumar Aug 05 '19 at 12:27
  • I have tried the code you have given, and also the link. I think you did not understand the scenario here. I'm not populating a ListView to show all Names and Numbers. Rather, User has picked some Contacts from a list of Contacts (which is implemented in library) and has `onActivityResult` where I can get the Names of Contacts picked by user but I also want to get phone numbers of picked Contacts. – Zubair Younas Aug 06 '19 at 07:28
0

First of all make sure you have written this line in your app manifest in permission

<uses-permission android:name="android.permission.READ_CONTACTS" />

And your main activity have code like this

new MultiContactPicker.Builder(MainActivity.this) //Activity/fragment context
                            .theme(R.style.MyCustomPickerTheme) //Optional - default: MultiContactPicker.Azure
                            .hideScrollbar(false) //Optional - default: false
                            .showTrack(true) //Optional - default: true
                            .searchIconColor(Color.WHITE) //Option - default: White
                            .setChoiceMode(MultiContactPicker.CHOICE_MODE_MULTIPLE) //Optional - default: CHOICE_MODE_MULTIPLE
                            .handleColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)) //Optional - default: Azure Blue
                            .bubbleColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)) //Optional - default: Azure Blue
                            .bubbleTextColor(Color.WHITE) //Optional - default: White
                            .setTitleText("Select Contacts") //Optional - default: Select Contacts
                            .setSelectedContacts("10", "5" / myList) //Optional - will pre-select contacts of your choice. String... or List<ContactResult>
                            .setLoadingType(MultiContactPicker.LOAD_ASYNC) //Optional - default LOAD_ASYNC (wait till all loaded vs stream results)
                            .limitToColumn(LimitColumn.NONE) //Optional - default NONE (Include phone + email, limiting to one can improve loading time)
                            .setActivityAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
                                    android.R.anim.fade_in,
                                    android.R.anim.fade_out) //Optional - default: No animation overrides
                            .showPickerForResult(CONTACT_PICKER_REQUEST);

Hope this will Help you

Kaushal Sachan
  • 1,175
  • 11
  • 8
  • Yes, I have already taken the Read Contacts permission obviously as I can get the names. The main activity code is just same as I am using, I've added it to the question. So Where are the phone numbers? – Zubair Younas Aug 06 '19 at 05:06
0

You just need to write the following code in onActivityResult, you can call phone numbers like this

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == CONTACT_PICKER_REQUEST){
        if(resultCode == RESULT_OK) {
            List<ContactResult> results = MultiContactPicker.obtainResult(data);
            Log.d("MyTag", results.get(0).getDisplayName());
                //contactsResults.addAll(MultiContactPicker.obtainResult(intent));
                List<ContactResult> results = MultiContactPicker.obtainResult(intent);
                if(!results.isEmpty()) {
                    for (int k = 0; k < results.size(); k++) {
                        System.out.println(results.get(k).getPhoneNumbers().get(0).getNumber());
                    }
                }
         else if(resultCode == RESULT_CANCELED){
            System.out.println("User closed the picker without selecting items.");
        }
    }
}}
Pallav Nagar
  • 627
  • 6
  • 5