}
public void onClickSaveLocation(final View v) {
- if (this.mMarker == null) {
+ if (this.mMarker != null) {
final LatLng position = this.mMarker.getPosition();
+ final TextView city = (TextView) this.findViewById(R.id.weather_map_city);
+ final TextView country = (TextView) this.findViewById(R.id.weather_map_country);
+ final String cityString = city.getText().toString();
+ final String countryString = country.getText().toString();
+
final DatabaseQueries query = new DatabaseQueries(this);
final WeatherLocation weatherLocation = query.queryDataBase();
if (weatherLocation != null) {
+ weatherLocation
+ .setCity(cityString)
+ .setCountry(countryString)
+ .setLatitude(position.latitude)
+ .setLongitude(position.longitude)
+ .setLastCurrentUIUpdate(null)
+ .setLastForecastUIUpdate(null);
query.updateDataBase(weatherLocation);
} else {
- final TextView city = (TextView) this.findViewById(R.id.weather_map_city);
- final TextView country = (TextView) this.findViewById(R.id.weather_map_country);
- final String cityString = city.getText().toString();
- final String countryString = country.getText().toString();
-
final WeatherLocation location = new WeatherLocation()
.setCity(cityString)
.setCountry(countryString)
country.setText(weatherLocation.getCountry());
final LatLng point = new LatLng(weatherLocation.getLatitude(), weatherLocation.getLongitude());
- this.mMap.clear();
- if (this.mMarker == null) {
- this.mMarker = this.mMap.addMarker(new MarkerOptions().position(point).draggable(true));
- } else {
+ if (this.mMarker != null) {
// Just one marker on map
- this.mMarker.setPosition(point);
+ this.mMarker.remove();
}
+ this.mMarker = this.mMap.addMarker(new MarkerOptions().position(point).draggable(true));
this.mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 5));
this.mMap.animateCamera(CameraUpdateFactory.zoomIn());
this.mMap.animateCamera(CameraUpdateFactory.zoomTo(8), 2000, null);
// Default values
String city = this.localContext.getString(R.string.city_not_found);
String country = this.localContext.getString(R.string.country_not_found);
- if (addresses == null || addresses.size() <= 0) {
+ if (addresses != null && addresses.size() > 0) {
if (addresses.get(0).getLocality() != null) {
city = addresses.get(0).getLocality();
}
final WeatherLocationDbQueries.DoQuery doQuery = new WeatherLocationDbQueries.DoQuery() {
@Override
- public WeatherLocation doQuery(final Cursor cursor) {
- final Calendar calendar = Calendar.getInstance();
-
+ public WeatherLocation doQuery(final Cursor cursor) {
final int id = cursor.getInt(cursor.getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation._ID));
final String city = cursor.getString(cursor.
getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY));
Date lastCurrentUIUpdate = null;
if (!cursor.isNull(cursor
.getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE))) {
- final int UNIXTime = cursor.getInt(cursor
+ final long javaTime = cursor.getLong(cursor
.getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE));
- calendar.setTimeInMillis((long)UNIXTime * 1000L);
- lastCurrentUIUpdate = calendar.getTime();
+ lastCurrentUIUpdate = new Date(javaTime);
}
Date lasForecastUIUpdate = null;
if (!cursor.isNull(cursor
.getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE))) {
- final int UNIXTime = cursor.getInt(cursor
+ final long javaTime = cursor.getLong(cursor
.getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE));
- calendar.setTimeInMillis((long)UNIXTime * 1000L);
- lastCurrentUIUpdate = calendar.getTime();
+ lasForecastUIUpdate = new Date(javaTime);
}
final double latitude = cursor.getDouble(cursor.
getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE));
values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
Date javaTime = weatherLocation.getLastCurrentUIUpdate();
if (javaTime != null) {
- final int UNIXTime = (int) javaTime.getTime() / 1000;
- values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, UNIXTime);
+ values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
}
javaTime = weatherLocation.getLastForecastUIUpdate();
if (javaTime != null) {
- final int UNIXTime = (int) javaTime.getTime() / 1000;
- values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, UNIXTime);
+ values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
}
values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
- values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE);
- values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE);
+ Date javaTime = weatherLocation.getLastCurrentUIUpdate();
+ if (javaTime != null) {
+ values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
+ } else {
+ values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE);
+ }
+ javaTime = weatherLocation.getLastForecastUIUpdate();
+ if (javaTime != null) {
+ values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
+ } else {
+ values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE);
+ }
values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
// TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
private long updateDataBase(final String table, final String[] selectionArgs,
final String selection, final ContentValues values) {
- final SQLiteDatabase db = this.mDbHelper.getReadableDatabase();
+ final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
try {
return db.update(table, values, selection, selectionArgs);
} finally {