I am trying to perform a direct search on user contacts, and I want to get the name, thumbnail and address (if any) of each corresponding contact.
Live search is performed during user input.
So he types ma and gets "martin", "matthews" ...
He will continue with the mat and will only see "matthews"
I try to achieve this with one request, for example the following, but I always get the contact number in the FORMATTED_ADRESS field. I have a problem with JOIN because I use ContactsContract.CommonDataKinds and ContactsContract.Contacts in the same request?
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 really solved my problem,
- request
Contacts._ID , Contacts.DISPLAY_NAME run the second query using Contacts._ID , as shown below:
Cursor 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 run a second request for each contact , which might not be the best way.
So my last question is: is it possible to get this working with the first request?
android contactscontract android-contacts
longilong
source share