Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import android.database.Cursor;
import android.database.CursorWindow;
import android.database.sqlite.SQLiteCursor;

import android.os.Build;

public class Main {
    static Object getObject(Cursor cursor, int columnIndex) throws Exception {
        int type = getType(cursor, columnIndex);
        Object value;
        switch (type) {
        case Cursor.FIELD_TYPE_BLOB:
            value = cursor.getBlob(columnIndex);
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            value = cursor.getFloat(columnIndex);
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            value = cursor.getLong(columnIndex);
            break;
        case Cursor.FIELD_TYPE_NULL:
            value = null;
            break;
        case Cursor.FIELD_TYPE_STRING:
            value = cursor.getString(columnIndex);
            break;
        default:
            value = cursor.getString(columnIndex);
        }
        return value;
    }

    private static int getType(Cursor cursor, int columnIndex) throws Exception {

        if (Build.VERSION.SDK_INT >= 11) {
            return cursor.getType(columnIndex);
        }

        SQLiteCursor sqLiteCursor = (SQLiteCursor) cursor;
        CursorWindow cursorWindow = sqLiteCursor.getWindow();
        int pos = cursor.getPosition();
        int type = -1;
        if (cursorWindow.isNull(pos, columnIndex)) {
            type = Cursor.FIELD_TYPE_NULL;
        } else if (cursorWindow.isLong(pos, columnIndex)) {
            type = Cursor.FIELD_TYPE_INTEGER;
        } else if (cursorWindow.isFloat(pos, columnIndex)) {
            type = Cursor.FIELD_TYPE_FLOAT;
        } else if (cursorWindow.isString(pos, columnIndex)) {
            type = Cursor.FIELD_TYPE_STRING;
        } else if (cursorWindow.isBlob(pos, columnIndex)) {
            type = Cursor.FIELD_TYPE_BLOB;
        }

        return type;
    }
}