何时使用 CursorJoiner / MatrixCursor / MergeCursor?

发布于 2024-11-01 17:10:57 字数 419 浏览 0 评论 0原文

我正在探索从两个或多个连接表中优雅地获取数据的不同方法。

我相信 MergeCursor,(Android 开发人员指南)似乎暗示可以(例如)通过连接两个查询(或将视图单独添加为行等)来替换等效的SQL UNION - 所以,这不是我想要的。

但我不知道 CursorJoinerMatrixCursor 到底是做什么的,或者如何使用它们。我已经查看了它们的来源(像往常一样),这对我来说毫无意义!我发现它们的使用示例并没有清楚地解释所产生的效果是什么。我真的很感激对它们以及它们可能使用的上下文的详细描述。

I'm exploring different ways to get data elegantly from two or more joined tables.

I believe MergeCursor, (Android Developer Guide) seems to imply that could (for example) replace an equivalent SQL UNION by concatenating two queries (or adding views individually as rows, etc) - so, not what I want.

But I'm at a loss as to what exactly CursorJoiner and MatrixCursor are for, or how to use them. I've looked at the source for them and (as usual) it means nothing to me! The examples I've found of them in use didn't clearly explain what the resulting effect was. I would really appreciate a good description of them, and the context they might be used in.

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

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

发布评论

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

评论(2

撩起发的微风 2024-11-08 17:10:57

正如您所指出的,MergeCursor 旨在“垂直”连接两个数据集,添加更多行。

CursorJoiner 旨在“水平”连接两个数据集,添加更多列。您可以将其视为类似于实现简单的SQL JOIN

MatrixCursor 允许您构建一些从纯数据中实现 Cursor 接口的东西,然后将其倒入二维数据模型中。

AbstractCursor 允许您将自己的自定义数据集包装在 Cursor 接口中,覆盖必要的方法。

MergeCursor, as you indicate, is designed to concatenate two data sets "vertically", adding more rows.

CursorJoiner is designed to concatenate two data sets "horizontally", adding more columns. You can think of this as akin to implementing a simple SQL JOIN.

MatrixCursor allows you to build something that implements the Cursor interface out of pure data, that you pour into a two-dimensional data model.

AbstractCursor allows you to wrap your own custom data set in a Cursor interface, overriding the methods that are necessary.

怪我入戏太深 2024-11-08 17:10:57

关于MatrixCursor,这里有一个使用示例。

这将返回数据的解密版本(在本例中仅一列,但在完整版本中许多列都被加密)。

public MatrixCursor decyrptedCard(long cardid) {
    EncryptDecrypt ed = new EncryptDecrypt(mContext,
            LoginActivity.getCurrentUserPassWord(),
            MainActivity.mCurrentUserid);
    String[] mcsrcolumns = {
            DBCardsTableConstants.CARDID.getDBColumnName(),
            DBCardsTableConstants.CARDNAMEONCARD.getDBColumnName()
    };
    MatrixCursor cnvcsr = new MatrixCursor(mcsrcolumns,0);
    String whereclause = DBCardsTableConstants.CARDID.getDBColumnName() +
            "=?";
    String[] whereargs = {Long.toString(cardid)};

    Cursor basecsr = db.query(DBCardsTableConstants.CARDS.getDBTableName(),
            null,
            whereclause,
            whereargs,
            null,null,null,null);
    if (!basecsr.moveToFirst()) {
        cnvcsr.addRow(new Object[]{0L,"NOTACARD"});
        return cnvcsr;
    }

    cnvcsr.addRow(new Object[]{
            basecsr.getLong(
                    basecsr.getColumnIndex(
                            DBCardsTableConstants.CARDID.getDBColumnName()
                    )),
            ed.decrypt(
                    basecsr.getString(
                            basecsr.getColumnIndex(
                                    DBCardsTableConstants.CARDNAMEONCARD.getDBColumnName()
                            )
                    )
            )
    });
    basecsr.close();
    return cnvcsr;
}

简而言之,它与使用普通游标没什么不同,只不过您在创建实例时定义了列。然后,您可以使用 addRow 方法添加行。

With regard to MatrixCursor, here's an example use.

This returns a decrypted version of the data (in this case just one column, but in the full version a number of columns are encrypted).

public MatrixCursor decyrptedCard(long cardid) {
    EncryptDecrypt ed = new EncryptDecrypt(mContext,
            LoginActivity.getCurrentUserPassWord(),
            MainActivity.mCurrentUserid);
    String[] mcsrcolumns = {
            DBCardsTableConstants.CARDID.getDBColumnName(),
            DBCardsTableConstants.CARDNAMEONCARD.getDBColumnName()
    };
    MatrixCursor cnvcsr = new MatrixCursor(mcsrcolumns,0);
    String whereclause = DBCardsTableConstants.CARDID.getDBColumnName() +
            "=?";
    String[] whereargs = {Long.toString(cardid)};

    Cursor basecsr = db.query(DBCardsTableConstants.CARDS.getDBTableName(),
            null,
            whereclause,
            whereargs,
            null,null,null,null);
    if (!basecsr.moveToFirst()) {
        cnvcsr.addRow(new Object[]{0L,"NOTACARD"});
        return cnvcsr;
    }

    cnvcsr.addRow(new Object[]{
            basecsr.getLong(
                    basecsr.getColumnIndex(
                            DBCardsTableConstants.CARDID.getDBColumnName()
                    )),
            ed.decrypt(
                    basecsr.getString(
                            basecsr.getColumnIndex(
                                    DBCardsTableConstants.CARDNAMEONCARD.getDBColumnName()
                            )
                    )
            )
    });
    basecsr.close();
    return cnvcsr;
}

In short it's little different to using a normal cursor, except you define the columns when you create an instance. You can then add rows with the addRow method.

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