Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Android - Sqlite search using LIKE and UPPER on International characters

  • 12-02-2013 06:40PM
    #1
    Registered Users, Registered Users 2 Posts: 18,272 ✭✭✭✭


    I am currently trying to run a search on the contacts database and where a value I pass in matches the display name in the contacts I want to return a cursor with the number of contacts that match.

    Currently this works OK except for international characters, here is how my code currently looks with constraint being a CharSequence taken from an EditText:
    String[] PROJECTION = new String[] {
                        ContactsContract.Contacts._ID,
                        ContactsContract.Contacts.DISPLAY_NAME,
                        ContactsContract.Contacts.HAS_PHONE_NUMBER,
                };
        
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
        		
        public Cursor runQuery(CharSequence constraint) {
        			  
        		String SELECTION = "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ")" 
        			+ " LIKE UPPER('%" + constraint+ "%') " + "and " + ContactsContract.Contacts.DISPLAY_NAME + " IS NOT NULL"
       	    	 + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
        		    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
        		    		PROJECTION, SELECTION, null, sortOrder);
        		    
        	return cur; 
        }
    

    As I said this works fine as I want the search to be case insensitive so the user doesn't have to type the exact name, with a normal English name this works fine however once international characters are introduced the search becomes case sensitive.

    I have done some research that suggests sqlite requires an ICU add on to make this work that is missing from Android. Or to use a collate when inserting into the database. However since I only read from the contacts content provider this is not an option for me.

    Does anyone know of a way to get the search in the code above to work with International characters while being case insensitive?

    The likes of Viber can do it, so there must be away? Maybe requires a work around?


Advertisement