Android contacts Display Name and Phone Number(s) in single database query?
Try this code:
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
if(people.moveToFirst()) {
do {
String name = people.getString(indexName);
String number = people.getString(indexNumber);
// Do work...
} while (people.moveToNext());
}
The contacts API is extremly tricky and has several implicit joins.
Read the ContactContract and ContactsProvider2 if you can afford the time.
What do you want? The tables are chained like this:
- Contact 1--* Raw Contact
- Raw Contact 1--* Phone Number (data table)
The API works like this: you select the bottom-most element (a phone number) and implicit join to the topmost element (contact).
You want to use the PHONE URI case (ContactsProvider2 / line 4377). This should select all phone numbers and join up to the contact.
Combine the PHONE uri with some UI magic (for grouping), request the DISPLAY_NAME and the PHONE number (DATA1?) and you should be able to solve the problem.