Android autocomplete with SQLite LIKE works partially

Viewed 1261

I have a Restaurants table in my SQLite DB that have the following records

| Name   | Latin_Name |
+--------+------------+
| Манаки | Manaki     |
+--------+------------+
| Енрико | Enriko     |
+---------------------+

Now I'm doing the search like this:

From my fragment:

String selection = DBHelper.COL_NAME + " LIKE ? OR " +
                    DBHelper.COL_LATIN_NAME + " LIKE ?";

String[] selectionArgs = {"%"+term+"%", "%"+term+"%"};

CursorLoader loader = new CursorLoader(getActivity(), 
                               DatabaseContentProvider.REST_CONTENT_URI, 
                               columns, selection, selectionArgs, null);

The content provider query method:

 public Cursor query(Uri uri, String[] projection, String selection,
                    String[] selectionArgs, String sortOrder) {

    db = dbHelper.getReadableDatabase();
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    switch(URI_MATCHER.match(uri)){
        case REST_LIST:
            builder.setTables(DBHelper.REST_TABLE);
            break;
        case REST_ID:
            builder.appendWhere(DBHelper.COL_ID + " = "
                    + uri.getLastPathSegment());
            break;
        default:
            throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
    Cursor cursor = builder.query(db, projection, selection, selectionArgs,
            null, null, sortOrder);


    cursor.setNotificationUri(getContext().getContentResolver(), uri);

    return cursor;
}

So pretty basic right? Now here comes the problem:

If the term that comes in is en I can see the Enriko restaurants among the others.

If I pass enri I can't see the Enriko as a result anymore.

Same goes for the Manaki restaurant I can see it until mana and after that (for manak term for ex) I can't see it in the results list.

I was debugging my ContentProvider and I realized that the cursor was empty, so the problem have to be at the database level, I guess.

Please help.

UPDATE:

Having the @laalto comments in mind I decided to do some test on the database. In the onCreate() method of my SQLiteOpenHelperI inserted only those two records in the table by hand and it worked.

Now the problem is that I have to insert 1300 records onCreate() from a json file shipped in the assets folder. Right now I'm parsing the json file create an ArrayList<Restaurant> then loop trough it and insert one record per object for all 1300 items.

That kind of insertion won't work with the SQLite LIKE method.

Are there any gotchas about this kind of populating the database?

Maybe I need to change file encoding (it is UTF-8 now) or maybe database's collation, or maybe getString() from JSONObject can be tweaked for the database?

2 Answers
Related