How to search for Contacts with the address (FORMATTED_ADDRESS) with one request?

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?

+7
android contactscontract android-contacts
source share
1 answer

Mmmm, it’s very sad that no one was able to answer my question and grab bonus points; - (

For the record, here is my working example. This solves the problem, but I still think it creates a lot of overload. In each user record ( afterTextchange ), I call getContactsDetailsQuery , which first gets all users with their identifier containing the request in their name ( cursor ), and then I launch another request ( detailCursor ) for each user address. To prevent overload, I added a restriction.

 public static List<SearchModel> getContactDetailsForQuery(Context context, String query, int limit) { final int CONTACT_ID_INDEX = 0; final int CONTACT_NAME_INDEX = 1; final int CONTACT_THUMBNAIL_INDEX = 2; //my custom model to hold my results List<SearchModel> results = new ArrayList<SearchModel>(); final String[] selectUser = new String[]{ Contacts._ID, Contacts.DISPLAY_NAME, Contacts.PHOTO_THUMBNAIL_URI}; String selection = Contacts.DISPLAY_NAME + " LIKE ?"; String[] selectionArgs = new String[]{"%" + query + "%"}; String sortOrder = Contacts.DISPLAY_NAME + " ASC"; Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, selectUser, selection, selectionArgs, sortOrder, null); int contactCounter = 0; if (cursor != null && cursor.moveToFirst()) { do { String contactID = cursor.getString(CONTACT_ID_INDEX); String displayName = cursor.getString(CONTACT_NAME_INDEX); String thumbnail = cursor.getString(CONTACT_THUMBNAIL_INDEX); //get user details with user id (this is the query i wanted to change in my question!!) Cursor detailCursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[]{ CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS}, ContactsContract.Data.CONTACT_ID + "=? AND " + CommonDataKinds.StructuredPostal.MIMETYPE + "=?", new String[]{String.valueOf(contactID), CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE}, null); if (detailCursor != null && detailCursor.moveToFirst()) { //special case: user has several address, query all of them do { String formattedAddress = detailCursor.getString(detailCursor.getColumnIndex(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS)); //user has serveral adress -> init model for each adress SearchModel contact = new SearchModel(); results.add(contact); contactCounter++; } while (detailCursor.moveToNext() && contactCounter < limit); } else { //user has no adress -> init model SearchModel contact = new SearchModel(); results.add(contact); contactCounter++; } detailCursor.close(); } while (cursor.moveToNext() && contactCounter < limit); } cursor.close(); return results; } 
0
source share

All Articles