I'm currently using the answer to this question: How to get all contacts first name, last name, email, phone number, etc without duplicates to retrieve the user contact list with, for each contact, all phone numbers, the firstname, lastname and the photo id.
The issue I've with that answer is it's creating one request per data wanted. It's creating performances issues on my app. I would like to retrieve all these information from a single request, but I've got a lot of difficulty to understand how Android's query are managed and where are stored data I'm looking for.
Example of what I've tried (I've done a lot of test without success, following one is the last one I've tried):
new CursorLoader(
this,
ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.HAS_PHONE_NUMBER + "!=0 AND (" + ContactsContract.Data.MIMETYPE + "=?)",
new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE},
ContactsContract.Data.CONTACT_ID)
;
And information I would like to retrieve:
int firstNameCol = cursor.getColumnIndex(CommonDataKinds.StructuredName.GIVEN_NAME);
int lastNameCol = cursor.getColumnIndex(CommonDataKinds.StructuredName.FAMILY_NAME);
int photoIdCol = cursor.getColumnIndex(CommonDataKinds.StructuredName.PHOTO_ID);
int phoneCol = cursor.getColumnIndex(Phone.NUMBER);
There is two issues with that request:
I only get one phone number per contact, however some of my contacts have several numbers and I would like to retrieve them
I'm not retrieving the firstname and the lastname (I get a weird number instead)
Please note that I don't want to retrieve the DISPLAY_NAME, I want both given_name and family_name. Please also note I would like to have one result per contact and not one result per phone number (i.e one result contain one to many phone numbers)
Any idea to do that in a single request?
EDIT: trying with another piece of code from @pskink but I get a "column '_id' does not exists" error
String[] projection = {
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
ContactsContract.CommonDataKinds.StructuredName.PHOTO_ID
};
String selection = ContactsContract.Data.MIMETYPE + " in (?, ?, ?, ?)";
String[] selectionArgs = {
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
ContactsContract.CommonDataKinds.StructuredName.PHOTO_ID
};
//String sortOrder = ContactsContract.Contacts.SORT_KEY_ALTERNATIVE;
//Uri uri = ContactsContract.CommonDataKinds.Contactables.CONTENT_URI;
Uri uri = ContactsContract.Data.CONTENT_URI;
// ok, let's work...
return new CursorLoader(
this, uri, projection, selection, selectionArgs, null
);