Beispiel #1
0
  /**
   * Get Single object from the database
   *
   * @param id Primary key
   * @return WeatherData object
   */
  public WeatherData getweather(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor =
        db.query(
            TABLE_WEATHER,
            new String[] {"id", NAME, LONG_NAME, LONG_NAME},
            "id" + "=?",
            new String[] {String.valueOf(id)},
            null,
            null,
            null,
            null);
    if (cursor != null) cursor.moveToFirst();

    WeatherData weatherData = new WeatherData();
    weatherData.setCity(cursor.getString(0));
    weatherData.setCoordLon(Double.parseDouble(cursor.getString(1)));
    weatherData.setCoordLon(Double.parseDouble(cursor.getString(2)));
    return weatherData;
  }
Beispiel #2
0
  /**
   * Return List of All records of WeatherData from DB
   *
   * @return List<WeatherData>
   */
  public List<WeatherData> getAllWeather() {
    List<WeatherData> weatherList = new ArrayList<WeatherData>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_WEATHER;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
      do {
        WeatherData weatherData = new WeatherData();
        weatherData.setID(cursor.getString(0));
        weatherData.setCity(cursor.getString(1));
        weatherData.setCoordLon(Double.parseDouble(cursor.getString(2)));
        weatherData.setCoordLat(Double.parseDouble(cursor.getString(3)));
        weatherList.add(weatherData);
      } while (cursor.moveToNext());
    }

    // return weather list
    return weatherList;
  }