1

I am trying to search for contacts on basis of their display name. I have taken the reference from sample code given on the android developer site.

https://developer.android.com/training/contacts-provider/retrieve-names.html

In this sample, search performs from the whole contacts details present in contact not only from contacts name.

For example if a user has an email address in his contacts, then the search is performing in matching the emails as well. or if a number is stored in home type, then while typing h, the number present in home category also shown up.

I want search to be limited to their display name.

     final static String SELECTION =
            (Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) +
                    "<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1";

These is the selecton clause that i am using taken from sample code.

Ritu Nagpal
  • 161
  • 1
  • 11

2 Answers2

1

Try This

Cursor cursor = getContentResolver().query(
            android.provider.ContactsContract.Contacts.CONTENT_URI,
            new String[] { ContactsContract.Contacts.PHOTO_ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.Contacts._ID },
            ContactsContract.Contacts.HAS_PHONE_NUMBER, null,
            ContactsContract.Contacts.DISPLAY_NAME);

This cursor gives all the contacts that have any phone number and then i save the unique ID in an ArrayList like this

cursor.moveToFirst();

    while (cursor.moveToNext()) {
        contactsID.add(cursor.getString(2));
    }

then on selecting the contact i find the contact numbers using this

Cursor cursor = getContentResolver()
                    .query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            new String[] {
                                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?",
                            new String[] { contactsID.get(position) }, null);
            contactNumbers = new ArrayList<String>();
            while (cursor.moveToNext()) {
                contactNumbers.add(cursor.getString(0));
                Log.d("number", cursor.getString(0));
            }
Ravish Sharma
  • 207
  • 2
  • 14
0

The code that does the filtering in the Retrieving a List of Contacts tutorial is here:

Uri contentUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mSearchString));

In the Contacts.CONTENT_FILTER_URI docs it says:

The filter string will be used to match various parts of the contact name

So that's not good for you, replace it with:

@Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
    Uri contentUri = Contacts.CONTENT_URI; // no longer filters
    String selection = Contacts.DISPLAY_NAME_PRIMARY + " LIKE %" + mSearchString + "%";

    return new CursorLoader(
            getActivity(),
            contentUri,
            PROJECTION,
            selection,
            null,
            null
    );
}
marmor
  • 27,641
  • 11
  • 107
  • 150