Put to Cursor Window - Android Database

Android examples for Database:Cursor

Description

Put to Cursor Window

Demo Code


//package com.book2s;
import android.database.Cursor;
import android.database.CursorWindow;
import android.os.Build;

public class Main {
    /**//from  w  w  w . ja  v  a 2  s .c o  m
     * This method was introduced by AndroidSQLiteServer to work around the lack of
     * {@link Cursor#getType(int)}} on Gingerbread.
     */
    private static boolean putToWindow(Cursor cursor, int column,
            CursorWindow window, int row) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            return putToWindowHoneycombAndBeyond(cursor, column, window,
                    row);
        } else {
            return putToWindowPreHoneycomb(cursor, column, window, row);
        }
    }

    private static boolean putToWindowHoneycombAndBeyond(Cursor cursor,
            int i, CursorWindow window, int position) {
        final int type = cursor.getType(i);
        final boolean success;
        switch (type) {
        case Cursor.FIELD_TYPE_NULL:
            success = window.putNull(position, i);
            break;

        case Cursor.FIELD_TYPE_INTEGER:
            success = window.putLong(cursor.getLong(i), position, i);
            break;

        case Cursor.FIELD_TYPE_FLOAT:
            success = window.putDouble(cursor.getDouble(i), position, i);
            break;

        case Cursor.FIELD_TYPE_BLOB: {
            final byte[] value = cursor.getBlob(i);
            success = value != null ? window.putBlob(value, position, i)
                    : window.putNull(position, i);
            break;
        }

        default: // assume value is convertible to String
        case Cursor.FIELD_TYPE_STRING: {
            final String value = cursor.getString(i);
            success = value != null ? window.putString(value, position, i)
                    : window.putNull(position, i);
            break;
        }
        }

        return success;
    }

    /**
     * This solution is consistent with how Gingerbread implemented
     * {@link android.database.AbstractCursor#fillWindow(int, android.database.CursorWindow)}.
     */
    private static boolean putToWindowPreHoneycomb(Cursor cursor, int i,
            CursorWindow window, int position) {
        String value = cursor.getString(i);
        final boolean success;
        if (value != null) {
            success = window.putString(value, position, i);
        } else {
            success = window.putNull(position, i);
        }
        return success;
    }
}

Related Tutorials