将一个Cursor的内容添加到另一个Cursor

时间:2011-01-04 06:02:16

标签: android cursor

我想连接两个游标,以便第二个Cursor的内容在加入后也会出现在第一个Cursor中。

这里是我的代码,

public final Uri AllImage_URI_Int = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
public final Uri AllAudio_URI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
cContentList = managedQuery(AllImage_URI_Int, null, null, null, MediaStore.Images.ImageColumns.TITLE);
cList_Int = managedQuery(AllImage_URI, null, null, null, MediaStore.Images.ImageColumns.TITLE);

我应该在这种情况下使用CursorJoiner吗?

我想将此Cursor传递给SimpleListAdapter?我如何加入这两个游标?

谢谢!

2 个答案:

答案 0 :(得分:17)

也许您可以使用MergeCursor wrapper将两个游标合并为一个游标,并将其传递给您的适配器。

答案 1 :(得分:3)

好吧,我自己解决了这个问题,并且通过AbstractCursor进行了扩展,继承了代码,

private final int ROWCACHESIZE = 64;
private int mRowNumCache[] = new int[ROWCACHESIZE];
private int mCursorCache[] = new int[ROWCACHESIZE];
private int mCurRowNumCache[][];
private int mLastCacheHit = -1;

public SortCursor(Cursor[] cursors, String sortcolumn)
{
    mCursors = cursors;

    int length = mCursors.length;
    mSortColumns = new int[length];
    for (int i = 0 ; i < length ; i++) {
        if (mCursors[i] == null) continue;

        mCursors[i].moveToFirst();

        // We don't catch the exception
        mSortColumns[i] = mCursors[i].getColumnIndexOrThrow(sortcolumn);
    }
    mCursor = null;
    String smallest = "";
    for (int j = 0 ; j < length; j++) {
        if (mCursors[j] == null || mCursors[j].isAfterLast())
            continue;
        String current = mCursors[j].getString(mSortColumns[j]);
        if (mCursor == null || current.compareToIgnoreCase(smallest) < 0) {
            smallest = current;
            mCursor = mCursors[j];
        }
    }

    for (int i = mRowNumCache.length - 1; i >= 0; i--) {
        mRowNumCache[i] = -2;
    }
    mCurRowNumCache = new int[ROWCACHESIZE][length];
}