Android query with case-insensitive cursor with LIKE operator (works for all locales)

I am trying to make a filter request:

public Cursor runQuery(CharSequence constraint) { return getActivity().getContentResolver().query( Phone.CONTENT_URI, new String[] {Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE, Phone._ID }, Phone.DISPLAY_NAME + " LIKE '" + constraint + "%'",// <-- problem here null, Phone.DISPLAY_NAME); } 

But the LIKE statement works case-sensitive for non-ascii characters (as stated in SQLite docs). Is there any way to do case-insensitive LIKE? (ps I test in Russian)

It does not work :

  • COLLATE NOCASE (UNICODE, LOCALIZED)
  • upper (), lower ()

Need help or advice. Thanks.

+8
android sqlite android-sqlite
source share
1 answer

Update: my solution in this situation

I do not solve the problem without register LIKE, but I found a solution for my specific situation, and it works even better than I expected :) Maybe this will be useful for you. So, to filter contacts with phones by name, use Phone.CONTENT_FILTER_URI

 @Override public Cursor runQuery(CharSequence constraint) { return mContext.getContentResolver().query( Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(constraint.toString())), //PROJECTION new String[] { Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE, Phone.PHOTO_ID}, //WHERE ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1", null, //ORDER BY null); } 

This provider performs advanced case-insensitive filtering, and also filters by phone number if the restriction is a number :)

+3
source share

All Articles