List of usage examples for android.database Cursor getDouble
double getDouble(int columnIndex);
From source file:edu.mit.mobile.android.locast.data.interfaces.LocatableUtils.java
public static Uri toGeoUri(Cursor c) { if (c.isNull(c.getColumnIndex(Locatable.COL_LATITUDE)) || c.isNull(c.getColumnIndex(Locatable.COL_LONGITUDE))) { return null; }// ww w .j av a 2s. c o m return Uri.parse("geo:" + c.getDouble(c.getColumnIndex(Locatable.COL_LATITUDE)) + "," + c.getDouble(c.getColumnIndex(Locatable.COL_LONGITUDE))); }
From source file:edu.mit.mobile.android.locast.data.Locatable.java
/** * Get the latitude/longitude from the row currently selected in the cursor. Requires Locatable.Columns._LATITUDE and Locatable.Columns._LONGITUDE to be selected. * @param c// w w w . j a va 2s . co m * @return */ public static Location toLocation(Cursor c) { final int lat_idx = c.getColumnIndex(Columns._LATITUDE); final int lon_idx = c.getColumnIndex(Columns._LONGITUDE); if (c.isNull(lat_idx) || c.isNull(lon_idx)) { return null; } final Location l = new Location("internal"); l.setLatitude(c.getDouble(lat_idx)); l.setLongitude(c.getDouble(lon_idx)); return l; }
From source file:Main.java
/** * Return the data stored in the cursor at the given index and given position * (ie the given row which the cursor is currently on) as null OR a String. * <p>//from w w w . j a va 2 s . c o m * NB: Currently only checks for Strings, long, int, and double. * * @param c * @param i * @return */ @SuppressLint("NewApi") public static String getIndexAsString(Cursor c, int i) { // If you add additional return types here be sure to modify the javadoc. if (i == -1) return null; if (c.isNull(i)) { return null; } switch (c.getType(i)) { case Cursor.FIELD_TYPE_STRING: return c.getString(i); case Cursor.FIELD_TYPE_FLOAT: { // the static version of this seems to have problems... Double d = c.getDouble(i); String v = d.toString(); return v; } case Cursor.FIELD_TYPE_INTEGER: { // the static version of this seems to have problems... Long l = c.getLong(i); String v = l.toString(); return v; } case Cursor.FIELD_TYPE_NULL: return c.getString(i); default: case Cursor.FIELD_TYPE_BLOB: throw new IllegalStateException("Unexpected data type in SQLite table"); } }
From source file:edu.mit.mobile.android.locast.data.interfaces.LocatableUtils.java
/** * Get the latitude/longitude from the row currently selected in the cursor. Requires * LocatableUtils.Locatable._LATITUDE and LocatableUtils.Locatable._LONGITUDE to be selected. * * @param c// w w w .jav a 2 s. c o m * @return */ public static Location toLocation(Cursor c) { final int lat_idx = c.getColumnIndex(Locatable.COL_LATITUDE); final int lon_idx = c.getColumnIndex(Locatable.COL_LONGITUDE); if (c.isNull(lat_idx) || c.isNull(lon_idx)) { return null; } final Location l = new Location("internal"); l.setLatitude(c.getDouble(lat_idx)); l.setLongitude(c.getDouble(lon_idx)); return l; }
From source file:org.jsharkey.sky.webservice.WebserviceHelper.java
/** * Perform a webservice query to retrieve and store the forecast for the * given widget. This call blocks until request is finished and * {@link Forecasts#CONTENT_URI} has been updated. */// www.j a v a 2 s . c o m public static void updateForecasts(Context context, Uri appWidgetUri, int days) throws ParseException { if (sUserAgent == null) { prepareUserAgent(context); } Uri appWidgetForecasts = Uri.withAppendedPath(appWidgetUri, AppWidgets.TWIG_FORECASTS); ContentResolver resolver = context.getContentResolver(); Cursor cursor = null; double lat = Double.NaN; double lon = Double.NaN; String countryCode = null; // Pull exact forecast location from database try { cursor = resolver.query(appWidgetUri, PROJECTION_APPWIDGET, null, null, null); if (cursor != null && cursor.moveToFirst()) { lat = cursor.getDouble(COL_LAT); lon = cursor.getDouble(COL_LON); countryCode = cursor.getString(COL_COUNTRY_CODE); } } finally { if (cursor != null) { cursor.close(); } } Log.d(TAG, "using country code=" + countryCode); // Query webservice for this location List<Forecast> forecasts = null; if (COUNTRY_US.equals(countryCode)) { forecasts = new NoaaSource().getForecasts(lat, lon, days); } else { forecasts = new MetarSource().getForecasts(lat, lon, days); } if (forecasts == null || forecasts.size() == 0) { throw new ParseException("No forecasts found from webservice query"); } // Purge existing forecasts covered by incoming data, and anything // before today long lastMidnight = ForecastUtils.getLastMidnight(); long earliest = Long.MAX_VALUE; for (Forecast forecast : forecasts) { earliest = Math.min(earliest, forecast.validStart); } resolver.delete(appWidgetForecasts, ForecastsColumns.VALID_START + " >= " + earliest + " OR " + ForecastsColumns.VALID_START + " <= " + lastMidnight, null); // Insert any new forecasts found ContentValues values = new ContentValues(); for (Forecast forecast : forecasts) { Log.d(TAG, "inserting forecast with validStart=" + forecast.validStart); values.clear(); values.put(ForecastsColumns.VALID_START, forecast.validStart); values.put(ForecastsColumns.TEMP_HIGH, forecast.tempHigh); values.put(ForecastsColumns.TEMP_LOW, forecast.tempLow); values.put(ForecastsColumns.CONDITIONS, forecast.conditions); values.put(ForecastsColumns.URL, forecast.url); if (forecast.alert) { values.put(ForecastsColumns.ALERT, ForecastsColumns.ALERT_TRUE); } resolver.insert(appWidgetForecasts, values); } // Mark widget cache as being updated values.clear(); values.put(AppWidgetsColumns.LAST_UPDATED, System.currentTimeMillis()); resolver.update(appWidgetUri, values, null, null); }
From source file:fr.eoit.parameter.Mining.java
private static AsteroidItemBean getItem(Cursor cursor) { AsteroidItemBean item = new AsteroidItemBean(); item.id = cursor.getInt(cursor.getColumnIndexOrThrow(ItemMaterials._ID)); item.name = cursor.getString(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_NAME)); item.batchSize = cursor.getInt(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_PORTION_SIZE)); item.volume = cursor.getDouble(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_VOLUME)); item.groupId = cursor.getInt(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_GROUP_ID)); return item;// w w w . j a v a 2s. c o m }
From source file:nz.co.wholemeal.christchurchmetro.Stop.java
private static ArrayList<Stop> doArrayListQuery(Context context, String query) { ArrayList<Stop> stops = new ArrayList<Stop>(); Log.d(TAG, "query: " + query); DatabaseHelper databaseHelper = new DatabaseHelper(context); SQLiteDatabase database = databaseHelper.getWritableDatabase(); Cursor cursor = database.rawQuery(query, null); try {// www . ja va 2 s. c om if (cursor.moveToFirst()) { do { Stop stop = new Stop(); stop.platformTag = cursor.getString(0); stop.platformNumber = cursor.getString(1); stop.name = cursor.getString(2); stop.roadName = cursor.getString(3); stop.latitude = cursor.getDouble(4); stop.longitude = cursor.getDouble(5); stops.add(stop); } while (cursor.moveToNext()); } } finally { cursor.close(); } Log.d(TAG, "stops.size() = " + stops.size()); database.close(); return stops; }
From source file:org.opendatakit.common.android.utilities.ODKCursorUtils.java
/** * Return the data stored in the cursor at the given index and given position * (ie the given row which the cursor is currently on) as null OR whatever * data type it is.//from ww w . j a v a 2 s . c om * <p> * This does not actually convert data types from one type to the other. * Instead, it safely preserves null values and returns boxed data values. If * you specify ArrayList or HashMap, it JSON deserializes the value into one * of those. * * @param c * @param clazz * @param i * @return */ @SuppressWarnings("unchecked") public static final <T> T getIndexAsType(Cursor c, Class<T> clazz, int i) { // If you add additional return types here be sure to modify the javadoc. try { if (i == -1) return null; if (c.isNull(i)) { return null; } if (clazz == Long.class) { Long l = c.getLong(i); return (T) l; } else if (clazz == Integer.class) { Integer l = c.getInt(i); return (T) l; } else if (clazz == Double.class) { Double d = c.getDouble(i); return (T) d; } else if (clazz == String.class) { String str = c.getString(i); return (T) str; } else if (clazz == Boolean.class) { // stored as integers Integer l = c.getInt(i); return (T) Boolean.valueOf(l != 0); } else if (clazz == ArrayList.class) { // json deserialization of an array String str = c.getString(i); return (T) ODKFileUtils.mapper.readValue(str, ArrayList.class); } else if (clazz == HashMap.class) { // json deserialization of an object String str = c.getString(i); return (T) ODKFileUtils.mapper.readValue(str, HashMap.class); } else { throw new IllegalStateException("Unexpected data type in SQLite table"); } } catch (ClassCastException e) { e.printStackTrace(); throw new IllegalStateException( "Unexpected data type conversion failure " + e.toString() + " in SQLite table "); } catch (JsonParseException e) { e.printStackTrace(); throw new IllegalStateException( "Unexpected data type conversion failure " + e.toString() + " on SQLite table"); } catch (JsonMappingException e) { e.printStackTrace(); throw new IllegalStateException( "Unexpected data type conversion failure " + e.toString() + " on SQLite table"); } catch (IOException e) { e.printStackTrace(); throw new IllegalStateException( "Unexpected data type conversion failure " + e.toString() + " on SQLite table"); } }
From source file:org.jsharkey.sky.WebserviceHelper.java
/** * Perform a webservice query to retrieve and store the forecast for the * given widget. This call blocks until request is finished and * {@link Forecasts#CONTENT_URI} has been updated. *///from w ww . jav a 2 s . c om public static void updateForecasts(Context context, Uri appWidgetUri, int days) throws ForecastParseException { Uri appWidgetForecasts = Uri.withAppendedPath(appWidgetUri, AppWidgets.TWIG_FORECASTS); ContentResolver resolver = context.getContentResolver(); Cursor cursor = null; double lat = Double.NaN; double lon = Double.NaN; // Pull exact forecast location from database try { cursor = resolver.query(appWidgetUri, PROJECTION_APPWIDGET, null, null, null); if (cursor != null && cursor.moveToFirst()) { lat = cursor.getDouble(COL_LAT); lon = cursor.getDouble(COL_LON); } } finally { if (cursor != null) { cursor.close(); } } // Query webservice for this location List<Forecast> forecasts = queryLocation(lat, lon, days); if (forecasts == null || forecasts.size() == 0) { throw new ForecastParseException("No forecasts found from webservice query"); } // Purge existing forecasts covered by incoming data, and anything // before today long lastMidnight = ForecastUtils.getLastMidnight(); long earliest = Long.MAX_VALUE; for (Forecast forecast : forecasts) { earliest = Math.min(earliest, forecast.validStart); } resolver.delete(appWidgetForecasts, ForecastsColumns.VALID_START + " >= " + earliest + " OR " + ForecastsColumns.VALID_START + " <= " + lastMidnight, null); // Insert any new forecasts found ContentValues values = new ContentValues(); for (Forecast forecast : forecasts) { Log.d(TAG, "inserting forecast with validStart=" + forecast.validStart); values.clear(); values.put(ForecastsColumns.VALID_START, forecast.validStart); values.put(ForecastsColumns.TEMP_HIGH, forecast.tempHigh); values.put(ForecastsColumns.TEMP_LOW, forecast.tempLow); values.put(ForecastsColumns.CONDITIONS, forecast.conditions); values.put(ForecastsColumns.URL, forecast.url); if (forecast.alert) { values.put(ForecastsColumns.ALERT, ForecastsColumns.ALERT_TRUE); } resolver.insert(appWidgetForecasts, values); } // Mark widget cache as being updated values.clear(); values.put(AppWidgetsColumns.LAST_UPDATED, System.currentTimeMillis()); resolver.update(appWidgetUri, values, null, null); }
From source file:Main.java
private static Object getCursorValue(Cursor cursor, Field field, String columnName) { Class<?> fieldType = field.getType(); if (fieldType.equals(int.class) || field.equals(Integer.class)) { return cursor.getInt(cursor.getColumnIndex(columnName)); } else if (fieldType.equals(String.class)) { return cursor.getString(cursor.getColumnIndex(columnName)); } else if (fieldType.equals(long.class) || field.equals(Long.class)) { return cursor.getLong(cursor.getColumnIndex(columnName)); } else if (fieldType.equals(float.class) || field.equals(Float.class)) { return cursor.getFloat(cursor.getColumnIndex(columnName)); } else if (fieldType.equals(double.class) || field.equals(Double.class)) { return cursor.getDouble(cursor.getColumnIndex(columnName)); } else if (fieldType.equals(long.class) || field.equals(Long.class)) { return cursor.getLong(cursor.getColumnIndex(columnName)); } else if (fieldType.equals(Date.class)) { long time = cursor.getLong(cursor.getColumnIndex(columnName)); Date date = new Date(time); return date; }/*from w w w.jav a 2s. c o m*/ return cursor.getString(cursor.getColumnIndex(columnName)); }