Filter items from cursor

I am trying to remove an item from a cursor object, and I am not sure how to do it (or if possible). I really do not want to remove the item from the database, just "filter" it and not display it, depending on user settings.

For example, FILTER_TEXT matches the preferences of the application and contains text that the cursor should contain, or it is deleted.

 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 sure this was the right way to handle this, but I cannot find a way to remove the item from the cursor ...

Any help would be appreciated, welcome!

+2
android android cursor
source share
2 answers

I think you might need to filter it out before it enters Cursor . Try modifying the query that generates it so that these elements never fall into it. This way, SQLite can filter out these unnecessary things before using more resources.

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

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

(This may require some tuning, my SQL is rusty.)

You can also look at SQLQueryBuilder .

As for the opportunity, I could not find anything in the documentation that would do what you wanted. I think Cursor is read-only.

0
source share
  • If you want to implement a list that can be filtered by the user, you can use the CursorAdapter and just set ListViews setTextFilterEnabled to true
  • You can use CursorLoader to load a new cursor in the background and change it when loading.
0
source share

All Articles