When to use CursorJoiner / MatrixCursor / MergeCursor?

前端 未结 2 862
鱼传尺愫
鱼传尺愫 2020-12-23 00:13

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

2条回答
  •  再見小時候
    2020-12-23 00:34

    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.

提交回复
热议问题