mardi 5 avril 2016

SQLite LIKE selection not working

I am trying to query my database using a LIKE statement like that %userCriteria%. I have a table with the following columns (stored in MySQLiteHelper) :

        MySQLiteHelper.COLUMN_DESCRIPTION,
        MySQLiteHelper.COLUMN_PUB_DATE,
        MySQLiteHelper.COLUMN_TITLE,
        MySQLiteHelper.COLUMN_USER_SPEC_NAME,
        MySQLiteHelper.COLUMN_LINK

I want to create a query which will return only the entries in TITLE column which contain a specific string.

My method that I am implementing (that is supposed to execute the query) is in my dataSourseClass looks like that:

 public ArrayList<RssItem> read(String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
    ArrayList<RssItem> tasks = new ArrayList<RssItem>();

    Cursor cursor = mDatabase.query(MySQLiteHelper.TABLE_NAME, new String[]{allColumns[3]}, selection, selectionArgs, groupBy, having, orderBy);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        RssItem task = cursorToTask(cursor);
        tasks.add(task);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return tasks;

}

allColumns:

private String[] allColumns = {
        MySQLiteHelper.COLUMN_ID,
        MySQLiteHelper.COLUMN_DESCRIPTION,
        MySQLiteHelper.COLUMN_PUB_DATE,
        MySQLiteHelper.COLUMN_TITLE,
        MySQLiteHelper.COLUMN_USER_SPEC_NAME,
        MySQLiteHelper.COLUMN_LINK
};

And this is how I am calling the read method in my Activity:

private void searchDatabase(String searchCriteriaTitle) {
    //clear the data from the adapter...
    mAdapter.clear();
    //get all of the stored records (during the parsing process)...
    List<RssItem> items = rssItemDataSource.read(MySQLiteHelper.COLUMN_TITLE + " LIKE ?",new String[]{searchCriteriaTitle},null,null,null);

    //if the search criteria is not empty string...
    if(!searchedItem.equals("")){
        for(int i = 0; i < items.size();i++) {
            //search from a match against the user's search criteria...

            mAdapter.add(items.get(i));

        }
    }
    //if the search criteria is am empty string...
    else{
        //read all of the
        List<RssItem> allItems = rssItemDataSource.getAllTasks();
       //add all...
        for(int i = 0; i < allItems.size();i++) {
            mAdapter.add(allItems.get(i));
        }
    }
}

Right now the list of returned entries is empty and I don't understand why. Any suggestions?

Aucun commentaire:

Enregistrer un commentaire