Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I use a solution that does the trick for all my different needs, and which is simpler than implementing Cursor.<br> Here is an example where I have to add extra "playlist" rows to a Cursor, retrieved from the Mediastore. I add rows at first indexes of the original Cursor :</p> <pre><code>public class CursorWithExtraPlaylists extends CursorWrapper { public static final int ID_COLUMN_INDEX = 0; public static final int NAME_COLUMN_INDEX = 1; private int mPos = -1; private Context mContext; private Playlist[] extras; public CursorWithExtraPlaylists(Cursor cursor, Context context, Playlist[] extras) { super(cursor); mContext = context; this.extras = extras; } @Override public int getCount() { return super.getCount() + extras.length; } @Override public int getPosition() { return mPos; } @Override public boolean moveToFirst() { return moveToPosition(0); } @Override public boolean moveToLast() { return moveToPosition(getCount() - extras.length); } @Override public boolean move(int offset) { return moveToPosition(mPos + offset); } @Override public boolean moveToPosition(int position) { // Make sure position isn't past the end of the cursor final int count = getCount(); if (position &gt;= count) { mPos = count; return false; } // Make sure position isn't before the beginning of the cursor if (position &lt; 0) { mPos = -1; return false; } // Check for no-op moves, and skip the rest of the work for them if (position == mPos) { return true; } mPos = position; if (mPos &gt; 0) { // ok, that concerns super cursor return super.moveToPosition(mPos - 1); } return true; } @Override public boolean moveToNext() { return moveToPosition(mPos + 1); } @Override public boolean moveToPrevious() { return moveToPosition(mPos - 1); } private boolean isPointingOnExtras() { if (-1 == mPos || getCount() == mPos) { throw new CursorIndexOutOfBoundsException(mPos, getCount()); } if (mPos &lt;= extras.length - 1) { // this is a request on an extra value return true; } return false; } private Object getExtraValue(int columnIndex) { switch (columnIndex) { case ID_COLUMN_INDEX: return extras[mPos].getId(); case NAME_COLUMN_INDEX: return extras[mPos].getName(mContext); } return null; } @Override public int getInt(int columnIndex) { if (isPointingOnExtras()) return (Integer) getExtraValue(columnIndex); int res = super.getInt(columnIndex); return res; } @Override public String getString(int columnIndex) { if (isPointingOnExtras()) return getExtraValue(columnIndex).toString(); return super.getString(columnIndex); } public static class Playlist { private int mId; private int mNameResId; public Playlist(int mId, int nameResId) { this.mId = mId; this.mNameResId = nameResId; } public int getId() { return mId; } public String getName(Context mContext) { return mContext.getResources().getString(mNameResId); } } } </code></pre> <p>Original cursor has 2 columns (int,String), so I construct it with an array of extra rows objects.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload