Skip to content

Instantly share code, notes, and snippets.

@KazaKago
Last active September 11, 2015 02:32
Show Gist options
  • Save KazaKago/b1873723b16bdf1d4bf2 to your computer and use it in GitHub Desktop.
Save KazaKago/b1873723b16bdf1d4bf2 to your computer and use it in GitHub Desktop.
Nullを考慮したCursor操作用ユーティリティクラス
import android.database.Cursor;
/**
* データベースカーソルのUtilクラス<br>
* 基本型もオブジェクト型として返却するためカラムがnullの場合にはnullを返す
*
* @author KCMEUser
*/
public class CursorUtil {
public static Integer getInteger(Cursor cursor, String columnName) {
return getInteger(cursor, cursor.getColumnIndexOrThrow(columnName));
}
public static Integer getInteger(Cursor cursor, int columnIndex) {
return (!cursor.isNull(columnIndex)) ? cursor.getInt(columnIndex) : null;
}
public static Long getLong(Cursor cursor, String columnName) {
return getLong(cursor, cursor.getColumnIndexOrThrow(columnName));
}
public static Long getLong(Cursor cursor, int columnIndex) {
return (!cursor.isNull(columnIndex)) ? cursor.getLong(columnIndex) : null;
}
public static Short getShort(Cursor cursor, String columnName) {
return getShort(cursor, cursor.getColumnIndexOrThrow(columnName));
}
public static Short getShort(Cursor cursor, int columnIndex) {
return (!cursor.isNull(columnIndex)) ? cursor.getShort(columnIndex) : null;
}
public static Float getFloat(Cursor cursor, String columnName) {
return getFloat(cursor, cursor.getColumnIndexOrThrow(columnName));
}
public static Float getFloat(Cursor cursor, int columnIndex) {
return (!cursor.isNull(columnIndex)) ? cursor.getFloat(columnIndex) : null;
}
public static Double getDouble(Cursor cursor, String columnName) {
return getDouble(cursor, cursor.getColumnIndexOrThrow(columnName));
}
public static Double getDouble(Cursor cursor, int columnIndex) {
return (!cursor.isNull(columnIndex)) ? cursor.getDouble(columnIndex) : null;
}
public static byte[] getBlob(Cursor cursor, String columnName) {
return getBlob(cursor, cursor.getColumnIndexOrThrow(columnName));
}
public static byte[] getBlob(Cursor cursor, int columnIndex) {
return (!cursor.isNull(columnIndex)) ? cursor.getBlob(columnIndex) : null;
}
public static String getString(Cursor cursor, String columnName) {
return getString(cursor, cursor.getColumnIndexOrThrow(columnName));
}
public static String getString(Cursor cursor, int columnIndex) {
return (!cursor.isNull(columnIndex)) ? cursor.getString(columnIndex) : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment