从 Android SQLite 游标中删除行

发布于 2024-10-22 06:11:18 字数 282 浏览 1 评论 0原文

我查询并获取结果集,但我需要执行一些在 SQLite WHERE 子句中不可能进行的计算,以确定 ListView 中显示的内容。如何从游标中删除某些行?我知道这与这个 Filter 是同一个问题来自 Cursor 的行,因此它们不会显示在 ListView 中,但这个答案没有帮助。如果没有更简单的方法可以提供示例吗?

I query and get a result set back, but I need to do some calculations that are impossible in the SQLite WHERE clause in order to determine what shows up in the ListView. How can I remove certain rows from the cursor? I know it is the same question as this Filter rows from Cursor so they don't show up in ListView but that answer does not help. Can an example be provided if there isn't a simpler way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

戈亓 2024-10-29 06:11:18

简单地保留光标中的所有行,然后使用自定义适配器在显示时隐藏不需要的行可能会起作用。例如,如果您扩展 CursorAdapter,那么您的 bindView 实现中可能会有类似这样的内容:

View v = view.findViewById(R.id.my_list_entry);
boolean keepThisRow = .......; // do my calculations
v.setVisibility(keepThisRow ? View.VISIBLE : View.GONE);

It might work to simply retain all the rows in the Cursor, but then use a custom adapter to hide the unwanted rows at display time. For example, if you extend CursorAdapter, then you might have something like this in your bindView implementation:

View v = view.findViewById(R.id.my_list_entry);
boolean keepThisRow = .......; // do my calculations
v.setVisibility(keepThisRow ? View.VISIBLE : View.GONE);
追星践月 2024-10-29 06:11:18

应该有更好的方法来做到这一点,但我最终做的是将我想要的每一行的 ID 存储在字符串 ArrayList 中,然后重新查询 where _id IN arraListOfIds.toString(),用括号替换方括号以适应SQL 语法。

// Get all of the rows from the database        
mTasksCursor = mDbHelper.fetchAllTasks();

ArrayList<String> activeTaskIDs = new ArrayList<String>();

// calculate which ones belong
// .....

if (!hasCompleted)             
                activeTaskIDs.add(mTasksCursor.getString(TaskerDBadapter.INDEX_ID));

// requery on my list of IDs
mTasksCursor = mDbHelper.fetchActiveTasks(activeTaskIDs);

public Cursor fetchActiveTasks(ArrayList<String> activeTaskIDs)
    {
        String inClause = activeTaskIDs.toString();
        inClause = inClause.replace('[', '(');
        inClause = inClause.replace(']', ')');

        Cursor mCursor = mDb.query(true, DATABASE_TABLE, columnStringArray(), 
                KEY_ROWID + " IN " + inClause, 
                null, null, null, null, null);

        if (mCursor != null) { mCursor.moveToFirst(); }

        return mCursor;
    }

There should be a better way to do this, but what I ended up doing is storing the ID of each row I wanted in a string ArrayList, and then requerying where _id IN arraListOfIds.toString(), replacing the square brackets with parentheses to fit SQL syntax.

// Get all of the rows from the database        
mTasksCursor = mDbHelper.fetchAllTasks();

ArrayList<String> activeTaskIDs = new ArrayList<String>();

// calculate which ones belong
// .....

if (!hasCompleted)             
                activeTaskIDs.add(mTasksCursor.getString(TaskerDBadapter.INDEX_ID));

// requery on my list of IDs
mTasksCursor = mDbHelper.fetchActiveTasks(activeTaskIDs);

public Cursor fetchActiveTasks(ArrayList<String> activeTaskIDs)
    {
        String inClause = activeTaskIDs.toString();
        inClause = inClause.replace('[', '(');
        inClause = inClause.replace(']', ')');

        Cursor mCursor = mDb.query(true, DATABASE_TABLE, columnStringArray(), 
                KEY_ROWID + " IN " + inClause, 
                null, null, null, null, null);

        if (mCursor != null) { mCursor.moveToFirst(); }

        return mCursor;
    }
捂风挽笑 2024-10-29 06:11:18
ContentResolver cr = getContentResolver();
        Cursor groupCur = cr.query(
                Groups.CONTENT_URI, // what table/content
                new String [] {Groups._ID, Groups.NAME},    // what columns
                "Groups.NAME NOT LIKE + 'System Group:%'", // where clause(s)
                null, // ???
                Groups.NAME + " ASC" // sort order
        );

上面的“What Columns”部分是您可以告诉光标要返回哪些行的地方。使用“null”会返回全部。

ContentResolver cr = getContentResolver();
        Cursor groupCur = cr.query(
                Groups.CONTENT_URI, // what table/content
                new String [] {Groups._ID, Groups.NAME},    // what columns
                "Groups.NAME NOT LIKE + 'System Group:%'", // where clause(s)
                null, // ???
                Groups.NAME + " ASC" // sort order
        );

The "What Columns" piece above is where you can tell the cursor which rows to return. Using "null" returns them all.

土豪我们做朋友吧 2024-10-29 06:11:18

我需要做一些计算
在 SQLite WHERE 中是不可能的
条款

这很难相信;我的经验是,SQL 可以让您查询几乎任何您需要的内容(SQLite 中的分层或递归查询除外)。如果您需要的某些功能不受支持,您可以使用 sqlite_create_function() 轻松添加它并在您的应用程序中使用它。或者,也许创造性地使用 SELECT 子句可以满足您的需求。

你能解释一下这些不可能的计算是什么吗?


编辑:没关系,查看 此网页 显示sqlite_create_function() 适配器全部由 Android SQLite 包装器封闭。这很烦人。

I need to do some calculations that
are impossible in the SQLite WHERE
clause

I find this very hard to believe; my experience has been that SQL will let you query for just about anything you'd ever need (with the exception of heirarchical or recursive queries in SQLite's case). If there's some function you need that isn't supported, you can add it easily with sqlite_create_function() and use it in your app. Or perhaps a creative use of the SELECT clause can do what you are looking for.

Can you explain what these impossible calculations are?


EDIT: Nevermind, checking out this webpage reveals that the sqlite_create_function() adapter is all closed up by the Android SQLite wrapper. That's annoying.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文