Get Value From Cursor - Android Database

Android examples for Database:Cursor Get

Description

Get Value From Cursor

Demo Code


//package com.java2s;

import android.database.Cursor;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static Object GetValueFromCursor(Cursor cursor,
            String columnName, Class propertyClass) {
        int columnIndex = cursor.getColumnIndexOrThrow(columnName);

        if (propertyClass.equals(int.class))
            return cursor.getInt(columnIndex);
        else if (propertyClass.equals(String.class))
            return cursor.getString(columnIndex);
        else if (propertyClass.equals(double.class))
            return cursor.getDouble(columnIndex);
        else if (propertyClass.equals(long.class))
            return cursor.getLong(columnIndex);
        else if (propertyClass.equals(float.class))
            return cursor.getFloat(columnIndex);
        else if (propertyClass.equals(Date.class))
            return ParseStringDate(cursor.getString(columnIndex),
                    "EEE MMM dd HH:mm:ss zzz yyyy");
        else/*from w  w w .j ava2  s . c o  m*/
            return cursor.getString(columnIndex);
    }

    private static Date ParseStringDate(String dateString,
            String dateFormatString) {
        Date parsedDate = new Date();
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    dateFormatString);
            parsedDate = dateFormat.parse(dateString);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return parsedDate;
    }
}

Related Tutorials