Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Android - Sqlite search using LIKE and UPPER on International characters

Options
  • 12-02-2013 6:40pm
    #1
    Registered Users 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