Search for contacts using the name, as well as the company (for example, in the Android application android 2.3.3)

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?

+4
source share
3 answers

Finally, I found a solution.

The problem was returning the contact id.

So, in the first case ( which is a search by name ), we must accept ContactsContract.Contacts._ID as the contact identifier.

and in the second case ( which is a company search ), since the Uri is different (in this case it is ContactsContract.Data.CONTENT_URI), as well as the selection criteria Data.MIMETYPE - Organization .CONTENT_ITEM_TYPE, we must use Organization.CONTACT_ID

Similarly, when searching by email, you should use ContactsContract.CommonDataKinds.Email.CONTACT_ID as the identifier of the contact. Similarly for other fields.

Using these Contacts, we can combine the search by name and company. We can remove duplicates in contact identifiers using the Set concept.

+2
source

use this function to find combinations .. maybe it will help you ..

 private Cursor getContactsByCompanyORname(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 + "%'" + "' OR '" ContactsContract.Contacts.DISPLAY_NAME + " like '" + temp + "%'"; String[] selectionArgs = null; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; return managedQuery(uri, proj, selection3, selectionArgs, sortOrder); } 

so change row selection3

0
source

It took a long time and a lot of disappointments! Below is a search by contact name, company and name:

 /** * Creates the Loader used to load contact data filtered by the * given Query String. */ private Loader<Cursor> createLoaderFiltered(String theQueryString) { final String[] COLS = new String[] {"contact_id", ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME}; final String LIKE = " LIKE '%" + theQueryString + "%'"; final Uri URI = ContactsContract.Data.CONTENT_URI; final String ORANIZATION_MIME = Organization.CONTENT_ITEM_TYPE; final String NAME_MIME = StructuredName.CONTENT_ITEM_TYPE; final String WHERE = "(" + Data.MIMETYPE + "='" + ORANIZATION_MIME + "'" + " AND (" + Organization.COMPANY + LIKE + " OR " + Organization.TITLE + LIKE + ")" + " AND " + ContactsContract.Contacts.DISPLAY_NAME + " NOT " + LIKE + ") OR (" + Data.MIMETYPE + "='" + NAME_MIME + "'" + " AND " + ContactsContract.Contacts.DISPLAY_NAME + LIKE + ")"; final String SORT = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; return new CursorLoader(getActivity(), URI, COLS, WHERE, null, SORT); } 

The "ContactsContract.Contacts.DISPLAY_NAME NOT LIKE" clause is necessary to eliminate duplicate lines for contacts that match both the contact name and company / name.

0
source

Source: https://habr.com/ru/post/1442010/


All Articles