I try to implement a live search over the users contacts, and I want to get the name, thumbnail and address (if there is one) of each matching contact.
The live search is running while the user is typing.
So he types ma and will get 'martin', 'matthews'...
He'll continue with mat and will only see 'matthews'
I try to achieve this with a single query like the following, but I always get the contact number in the FORMATTED_ADRESS
field. I guess I have a JOIN
problem, because I'm using ContactsContract.CommonDataKinds
and ContactsContract.Contacts
in the same query?
public static List<ContactModel> getContactsForQuery(Context context, String query) {
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS
};
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String selection = ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%" + query + "%'";
Cursor cursor = context.getContentResolver().query(uri, projection, selection, null,null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(0);
String thumbail = cursor.getString(1);
String formattedADress = cursor.getString(2);
}
while (cursor.moveToNext());
}
I actually solved my issue, with
- querying for
Contacts._ID
,Contacts.DISPLAY_NAME
start a second query with the
Contacts._ID
like the followingCursor detailCursor = context.getContentResolver().query( ContactsContract.Data.CONTENT_URI, new String[]{ CommonDataKinds.StructuredPostal.STREET, CommonDataKinds.StructuredPostal.CITY, CommonDataKinds.StructuredPostal.POSTCODE }, ContactsContract.Data.CONTACT_ID + "=? AND " + CommonDataKinds.StructuredPostal.MIMETYPE + "=?", new String[]{ String.valueOf(contactID), CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE }, null);
but this will start a second query for every contact, which might not be the best approach.
So my final question is: is it possible to get this work with the first query?