Reputation: 953
Though below code giving me multiple contacts I would like to get contact names also.
public void pickContact() {
try {
Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU");
phonebookIntent.putExtra("additional", "phone-multi");
startActivityForResult(phonebookIntent, 1);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onActivityResult(int reqCode, int resultCode, Intent data) {
if (reqCode != 1) {
Log.d("Val1", reqCode + "");
return;
}
if(resultCode != Activity.RESULT_OK) {
Log.d("Val2", resultCode + ":" + Activity.RESULT_OK);
return;
}
Bundle contactUri = data.getExtras();
if (null == contactUri) {
Log.d("Val1", reqCode + "null");
return;
}
ArrayList<String> contacts = (ArrayList<String>)contactUri.get("result");
Log.d("values", contacts.get(0) + ":" + contacts.size());
}
Upvotes: 2
Views: 736
Reputation: 953
If any one still not getting answer, I got one.. If you use below code you will get all contacts and names, here you have to write your own adapter to display them, make user to select multiple contacts. Thank you :)
public static ArrayList<Contact> getContactsList(Context context) {
ArrayList<Contact> contacts=new ArrayList<>();
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts.add(new Contact(name,phoneNumber));
}
phones.close();
return contacts;
}
Upvotes: 3