When to use CursorJoiner / MatrixCursor / MergeCursor?

五迷三道 提交于 2019-11-29 19:52:11

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!