Retrieve a Long from a cursor, or null if a long could not be retrieved. - Android Database

Android examples for Database:Cursor Get

Description

Retrieve a Long from a cursor, or null if a long could not be retrieved.

Demo Code


import java.util.ArrayList;
import java.util.Locale;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class Main{
    /**/*w ww.  j  a  va2 s.co m*/
     * Retrieve a Long from a cursor, or null if a long could not be retrieved.
     *
     * @param cursor The cursor from which to retrieve the value. Must be at a valid position.
     * @param columnIndex The index of the column from which to retrieve the value.
     * @return The Long value, or null.
     */
    public static Long safeGetLongFromCursor(Cursor cursor, int columnIndex) {
        try {
            return cursor.getLong(columnIndex);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * Retrieve a Long from a cursor, or null if a long could not be retrieved.
     *
     * @param cursor The cursor from which to retrieve the value. Must be at a valid position.
     * @param columnName The name of the column from which to retrieve the value.
     * @return The Long value, or null.
     */
    public static Long safeGetLongFromCursor(Cursor cursor,
            String columnName) {
        return safeGetLongFromCursor(cursor,
                cursor.getColumnIndex(columnName));
    }
    /**
     * Retrieve a long from a cursor, or a default value.
     *
     * @param cursor The cursor from which to retrieve the value. Must be at a valid position.
     * @param columnIndex The index of the column from which to retrieve the value.
     * @param defaultValue The value to return if none was retrieved.
     * @return The long value, or the default.
     */
    public static long safeGetLongFromCursor(Cursor cursor,
            int columnIndex, long defaultValue) {
        try {
            return cursor.getLong(columnIndex);
        } catch (Exception e) {
            e.printStackTrace();
            return defaultValue;
        }
    }
    /**
     * Retrieve a long from a cursor, or a default value if a long could not be retrieved.
     *
     * @param cursor The cursor from which to retrieve the value. Must be at a valid position.
     * @param columnName The name of the column from which to retrieve the value.
     * @param defaultValue The value to return if none was retrieved.
     * @return The long value, or the default.
     */
    public static long safeGetLongFromCursor(Cursor cursor,
            String columnName, long defaultValue) {
        return safeGetLongFromCursor(cursor,
                cursor.getColumnIndex(columnName), defaultValue);
    }
}

Related Tutorials