When to use CursorJoiner / MatrixCursor / MergeCursor?
Asked Answered
P

2

37

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.

Pinochle answered 18/4, 2011 at 10:28 Comment(0)
F
73

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.

Fischer answered 18/4, 2011 at 12:54 Comment(3)
Thank you, I'm now clear on MergeCursor and CursorJoiner. I'm somewhat mystified still about MatrixCursor. Perhaps I should read up on extending AbstractCursor.Pinochle
@Jodes: "MatrixCursor is useful if you have a collection of data that is not in the database, and you want to create a cursor for it." Android Advanced CursorsEpimenides
Extremely useful answer. The interesting point here is that CursorJoiner is not a Cursor object, thus is (almost) completely useless because if you want to access to the joined data like in a Cursor you need to manually create a MatrixCursor from it (no utils or other methods available to do this task btw).Cell
M
0

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.

Mews answered 15/9, 2017 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.