How to clone or freeze an Android database cursor

前端 未结 3 985
再見小時候
再見小時候 2021-01-06 17:00

I have a custom cursor adapter, and I would like to pass each \'row\' of the cursor back to the application (via a registered callback which is working).

I know I co

3条回答
  •  [愿得一人]
    2021-01-06 17:28

    Consider you have a cursor named data and cursor have columns "title" and "author" and both these columns have string values. The below code shows how to copy cursor named data to cursor named matrixCursor.

    String[] PROJECTION = new String[]{"title","author"};
    MatrixCursor matrixCursor = new MatrixCursor(PROJECTION);
    int i = data.getColumnCount();
    if (data.moveToFirst()) {
                    do {
                    Object[] currRow = new Object[i];
                    currRow[0] = data.getString(0);
                    currRow[1] = data.getString(1);
                    matrixCursor.addRow(currRow);
                    } while (data.moveToNext());
                }
    

提交回复
热议问题