Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to make a filtering query:

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 LIKE operator works in case-sensitive way for non-ascii chars (as SQLite docs says). Is there a way to make case-insensitive LIKE? (p.s. i test on russian symbols)

Things that not works:

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

Need help or advice. Thank you.

like image 669
j2esu Avatar asked Feb 13 '13 21:02

j2esu


1 Answers

Update: my solution in this situation

I haven't solve a problem of case-insensitive LIKE, but I found a solution for my specific situation, and it works even better than i expected :) May be it'll be usefull 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 perform advanced case-insensitive filtering and also filters by phone number if constraint is number :)

like image 175
prabhu Avatar answered Oct 06 '22 16:10

prabhu