How to clone or freeze an Android database cursor

前端 未结 3 1002
再見小時候
再見小時候 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:08

    Based on Anand's answer, this should work without having to specify the column names / projection:

    public static Cursor cloneCursor(Cursor oldCursor) {
    
        if (oldCursor == null) {
    
            return null;
    
        }
        else {
    
            /**
             * Remember the cursor position
             */
            int originalCursorPosition = oldCursor.getPosition();
    
            String[] projection = oldCursor.getColumnNames();
            MatrixCursor newCursor = new MatrixCursor(projection);
    
            int numColumns = oldCursor.getColumnCount();
    
            while (oldCursor.moveToNext()) {
    
                /**
                 * Create the new row object
                 */
                Object[] newRow = new Object[numColumns];
    
                /**
                 * Populate each column in the new row
                 */
                for (int columnIndex = 0; columnIndex < numColumns; columnIndex++) {
    
                    /**
                     * Detect the field type
                     */
                    int fieldType = oldCursor.getType(columnIndex);
    
                    /**
                     * Use the field type to populate the row correctly
                     */
                    if (fieldType == Cursor.FIELD_TYPE_BLOB) {
                        newRow[columnIndex] = oldCursor.getBlob(columnIndex);
                    }
                    else if (fieldType == Cursor.FIELD_TYPE_FLOAT) {
                        newRow[columnIndex] = oldCursor.getDouble(columnIndex);
                    }
                    else if (fieldType == Cursor.FIELD_TYPE_INTEGER) {
                        newRow[columnIndex] = oldCursor.getLong(columnIndex);
                    }
                    else if (fieldType == Cursor.FIELD_TYPE_STRING) {
                        newRow[columnIndex] = oldCursor.getString(columnIndex);
                    }
                    else if (fieldType == Cursor.FIELD_TYPE_NULL) {
                        newRow[columnIndex] = null;
                    }
                    else {
                        throw new RuntimeException("Unknown fieldType (" + fieldType + ") for column" + columnIndex);
                    }
    
                }
    
                /**
                 * Add the new row to the new cursor
                 */
                newCursor.addRow(newRow);
    
            }
    
            /**
             * Move both cursors to the position that oldCursor was in before this method was called
             */
            oldCursor.moveToPosition(originalCursorPosition);
            newCursor.moveToPosition(originalCursorPosition);
    
            /**
             * Return the cloned cursor
             */
            return newCursor;
    
        }
    
    }
    

    UPDATE

    getInt(...) changed to getLong(...).

    getFloat(...) changed to getDouble(...)

提交回复
热议问题