过滤掉光标中的项目

发布于 2024-11-18 01:31:21 字数 585 浏览 3 评论 0原文

我正在尝试从光标对象中删除一个项目,但我不确定如何执行此操作(或者是否可能)。我实际上不想从数据库中删除该项目,只是“过滤”它而不显示它,具体取决于用户设置。

例如,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪之魂 2024-11-25 01:31:21

我认为您可能想在它进入光标之前对其进行过滤。尝试更改生成它的查询,以便这些项目永远不会进入其中。这样,SQLite 可以在那些不需要的东西耗尽更多资源之前过滤掉它们。

您可以通过将内容发送到“查询”的“选择”参数来完成此操作,例如:

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

(这可能需要一些调整,我的 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:

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.

烟火散人牵绊 2024-11-25 01:31:21
  • 如果您想实现一个可由用户过滤的列表,您可以使用 CursorAdapter 并只需设置 ListViews setTextFilterEnabled 为 true
  • 您可以使用 CursorLoader,在后台加载新光标并在加载时将其交换。
  • 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文