4

I have developed a simple contacts application and also implemented search using name. But now I want to search using both name and company (just like how default android contacts app does). I am able to search separately using company but couldn't get other contact information because the contact id returned is different...I have pasted my code below.

Code to get contacts using name search : (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 to get 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 (i.e. name search), I am getting a cursor with information like contact id, name. Using contact id, I display information of the contact like photo, email in view contact page.

In the second case (i.e. company search), I get a cursor with information contact id, name and company. But here the contact id returned for the same contacts is different from that returned in the first case. So I cannot get other info of the contact like photo, email etc using this contact id.

If the contact id of a contact is same in both case 1 and case 2, I can integrate the two searches into one by removing duplicates. But this is not the case here.

So my question is how can I find contact information from the second case if contact id is different and how can i combine the two searches?

DSP
  • 487
  • 2
  • 5
  • 18

3 Answers3

2

At last I have found the solution.

The problem was with the contact id returned.

So in the first case (that is search by name), we have to take ContactsContract.Contacts._ID as the id of the contact.

and in the second case (that is search by company), since the Uri is different (in this case it is ContactsContract.Data.CONTENT_URI) and also the selection criteria Data.MIMETYPE is Organization.CONTENT_ITEM_TYPE, we have to use Organization.CONTACT_ID

Similarly when you search using Email, you have to use ContactsContract.CommonDataKinds.Email.CONTACT_ID as the contact id. Similarly for other fields.

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

DSP
  • 487
  • 2
  • 5
  • 18
0

use this function for combine search.. may it 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 String selection3

Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
  • I have used your code with minor syntax changes in selection3 but it is not working... – DSP Oct 31 '12 at 05:14
0

This took a long time and a lot of frustration! The following does a search by contact name, company, and title:

/**
 * 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 needed to eliminate duplicate rows for contacts which match on both the contact name and the company/title.

dazed
  • 352
  • 3
  • 9