Android Cursor Put putToWindow(Cursor cursor, int column, CursorWindow window, int row)

Here you can find the source of putToWindow(Cursor cursor, int column, CursorWindow window, int row)

Description

This method was introduced by AndroidSQLiteServer to work around the lack of Cursor#getType(int) } on Gingerbread.

License

Apache License

Declaration

private static boolean putToWindow(Cursor cursor, int column,
        CursorWindow window, int row) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import android.database.Cursor;
import android.database.CursorWindow;
import android.os.Build;

public class Main {
    /**/*from   ww  w  .j a 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

  1. putToWindowHoneycombAndBeyond(Cursor cursor, int i, CursorWindow window, int position)
  2. putToWindowPreHoneycomb(Cursor cursor, int i, CursorWindow window, int position)
  3. cursorFillWindow(final Cursor cursor, int position, final CursorWindow window)