Filter out items from cursor

…衆ロ難τιáo~ 提交于 2019-12-11 02:15:35

问题


I'm trying to remove an item from a cursor object, and I'm not sure how to do it (or if it's possible). I don't actually want to remove the item from the database, just 'filter' it and not display it, depending on user settings.

For example here, FILTER_TEXT is from the application preferences and it contains text that the cursor must contain or else it is removed.

Cursor mCursor = mDB.query(dbTable, new String[] {KEY_ROWID, KEY_NAME,
            KEY_URL}, null, null, null, null, null);

    if (mCursor.moveToFirst()) {
        do {
            if (!mCursor.getString(1).contains(FILTER_TEXT)) {
                // Remove cursor item here
            }
        } while (mCursor.moveToNext());
    }

I was fairly sure this was the right way to tackle this, but I can't find any way to remove an item from a cursor...

Any help would be appreciated, cheers!


回答1:


I think you might want to filter it before it even gets in to the Cursor. Try altering the query that generates it so that those items never even get in to it. This way, SQLite can filter out those unneeded things before they use up any more resources.

You can do this by sending things to the `selection' parameter of 'query', something like:

KEY_NAME + " LIKE '%" + FILTER_TEXT + "%'"

(That may need some tweaking, my SQL is rusty.)

You may also want to look in to SQLQueryBuilder.

As to if it's possible, I couldn't find anything in the documentation that would do what you wanted. I think Cursor is read-only.




回答2:


  • If you want to implement a i.e. a List that is filterable by the user you can use a CursorAdapter and just set the ListViews setTextFilterEnabled to true
  • You can use a CursorLoader, to load a new cursor in the background and swap it in when its loaded.


来源:https://stackoverflow.com/questions/6528452/filter-out-items-from-cursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!