过滤掉光标中的项目
我正在尝试从光标对象中删除一个项目,但我不确定如何执行此操作(或者是否可能)。我实际上不想从数据库中删除该项目,只是“过滤”它而不显示它,具体取决于用户设置。
例如,FILTER_TEXT
来自应用程序首选项,它包含光标必须包含的文本,否则将被删除。
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'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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可能想在它进入光标之前对其进行过滤。尝试更改生成它的查询,以便这些项目永远不会进入其中。这样,SQLite 可以在那些不需要的东西耗尽更多资源之前过滤掉它们。
您可以通过将内容发送到“查询”的“选择”参数来完成此操作,例如:
(这可能需要一些调整,我的 SQL 很生疏。)
您可能还想查看 < a href="http://developer.android.com/reference/android/database/sqlite/SQLiteQueryBuilder.html" rel="nofollow">
SQLQueryBuilder
。至于是否可能,我在 文档 中找不到任何内容那会做你想做的事。我认为
Cursor
是只读的。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:
(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.