Create MatrixCursor From Cursor - Android Database

Android examples for Database:Cursor

Description

Create MatrixCursor From Cursor

Demo Code


//package com.java2s;
import android.database.Cursor;
import android.database.MatrixCursor;

public class Main {
    public static MatrixCursor matrixCursorFromCursor(Cursor cursor) {
        if (cursor == null) {
            return null;
        }//w  ww  .ja  v a  2 s .  co  m

        String[] columnNames = cursor.getColumnNames();
        if (columnNames == null) {
            columnNames = new String[] {};
        }
        MatrixCursor newCursor = new MatrixCursor(columnNames);
        int numColumns = cursor.getColumnCount();
        String data[] = new String[numColumns];
        cursor.moveToPosition(-1);
        while (cursor.moveToNext()) {
            for (int i = 0; i < numColumns; i++) {
                data[i] = cursor.getString(i);
            }
            newCursor.addRow(data);
        }
        return newCursor;
    }
}

Related Tutorials