I developed a simple contacts application and also searched using a name. But now I want to search using the name and company (just like the default Android app does). I can search separately using the company, but could not get other contact information, because the contact ID returned is different ... I inserted the code below.
Code for getting contacts using search by name: (the search string is obtained from edittext using textchangedlistener)
private Cursor getContactsByName(String temp) { Uri uri = ContactsContract.Contacts.CONTENT_URI; String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, }; String selection = ContactsContract.Contacts.DISPLAY_NAME + " like '" + temp + "%'"; String[] selectionArgs = null; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; return managedQuery(uri, projection, selection, selectionArgs, sortOrder); }
Code for getting contacts using company search: (the search string is obtained from edittext using textchangedlistener)
private Cursor getContactsByCompany(String temp) { Uri uri = ContactsContract.Data.CONTENT_URI; String[] proj = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, Organization.COMPANY}; String selection3 = Data.MIMETYPE + "='" + Organization.CONTENT_ITEM_TYPE + "' AND " + Organization.COMPANY + " like '" + temp + "%'"; String[] selectionArgs = null; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; return managedQuery(uri, proj, selection3, selectionArgs, sortOrder); }
In the first case (for example, search by name) I get a cursor with information, such as contact identifier, name. Using the contact ID, I display contact information, such as a photo, email address on the contact page.
In the second case (i.e. searching for a company) I get a cursor with contact information identifier, name and company. But here the contact identifier returned for the same contacts is different from the one returned in the first case. Therefore, I can’t get other contact information such as photo, email address, etc. Using this contact id.
If the contact’s contact identifier is the same in both case 1 and case 2, I can integrate the two searches into one, removing duplicates. But here it is not.
So my question is how to find the contact information from the second case, if the contact ID is different and how can I combine the two requests?