get Datum from Cursor - Android Database

Android examples for Database:Cursor Get

Description

get Datum from Cursor

Demo Code


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

public class Main {
    public static Object getDatum(Cursor cursor, String column) {
        int index = cursor.getColumnIndex(column);
        return getDatum(cursor, index);
    }/* ww w .  j  ava  2 s  .  c  o  m*/

    public static Object getDatum(Cursor cursor, int index) {
        int type = cursor.getType(index);
        Object data;
        switch (type) {
        case Cursor.FIELD_TYPE_STRING:
            data = cursor.getString(index);
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            // Because INTEGER can store Long and Integer, we need to use Long, and cast later
            data = cursor.getLong(index);
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            data = cursor.getDouble(index);
            break;
        default:
            data = null;
            break;
        }
        return data;
    }
}

Related Tutorials