<string name="app_name">Weather Information</string>
<string name="action_settings">Settings</string>
<string name="action_search">City,country</string>
+ <string name="action_get_weather">Get weather</string>
<string name="button_weather">Get weather</string>
<string name="uri_api_coord">http://api.openweathermap.org/data/{0}/weather?lat={1}&lon={2}</string>
<string name="uri_api_city">http://api.openweathermap.org/data/{0}/weather?q={1}</string>
<string name="text_field_tem">Temperature:</string>
<string name="text_field_tem_min">Min temperature:</string>
<string name="text_field_tem_max">Max temperature:</string>
- <string name="text_field_cloudiness">Cloudiness:</string>
+ <string name="text_field_cloudiness">Cloudiness in %:</string>
<string name="text_field_rain_time">Rain time:</string>
<string name="text_field_rain_amount">Rain amount:</string>
<string name="text_field_wind_speed">Wind speed:</string>
<string name="error_dialog_connection_tiemout">Connection error timeout</string>
<string name="error_dialog_generic_error">Impossible to receive weather data.</string>
<string name="icon_weather_description">Icon weather</string>
- <string name="header_action_bar">Weather Information</string>
+ <string name="header_action_bar">City,country</string>
<string name="weather_preferences_units_key">weather_preferences_units</string>
<string name="weather_preferences_units_fahrenheit">Fahrenheit</string>
<string name="weather_preferences_units_celsius">Celsius</string>
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Date;
+import java.util.List;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import android.app.Fragment;
import android.content.SharedPreferences;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
+import android.widget.ImageView;
import android.widget.ListView;
import de.example.exampletdd.R;
import de.example.exampletdd.activityinterface.ErrorMessage;
import de.example.exampletdd.service.WeatherService;
public class WeatherInformationDataFragment extends Fragment implements OnClickButtons {
- private WeatherDataAdapter mAdapter;
private boolean isFahrenheit;
final ListView listWeatherView = (ListView) this.getActivity().findViewById(
R.id.weather_data_list_view);
- this.mAdapter = new WeatherDataAdapter(this.getActivity(),
+ final WeatherDataAdapter adapter = new WeatherDataAdapter(this.getActivity(),
R.layout.weather_data_entry_list);
- final Collection<WeatherDataEntry> entries = new ArrayList<WeatherDataEntry>();
- entries.add(new WeatherDataEntry(this.getString(R.string.text_field_description), ""));
- entries.add(new WeatherDataEntry(this.getString(R.string.text_field_tem), ""));
- entries.add(new WeatherDataEntry(this.getString(R.string.text_field_tem_max), ""));
- entries.add(new WeatherDataEntry(this.getString(R.string.text_field_tem_min), ""));
- entries.add(new WeatherDataEntry(this.getString(R.string.text_field_sun_rise), ""));
- entries.add(new WeatherDataEntry(this.getString(R.string.text_field_sun_set), ""));
- entries.add(new WeatherDataEntry(this.getString(R.string.text_field_cloudiness), ""));
- entries.add(new WeatherDataEntry(this.getString(R.string.text_field_rain_time), ""));
- entries.add(new WeatherDataEntry(this.getString(R.string.text_field_rain_amount), ""));
- entries.add(new WeatherDataEntry(this.getString(R.string.text_field_wind_speed), ""));
- entries.add(new WeatherDataEntry(this.getString(R.string.text_field_humidity), ""));
-
- this.mAdapter.addAll(entries);
- listWeatherView.setAdapter(this.mAdapter);
+ final Collection<WeatherDataEntry> entries = createEmptyEntriesList();
+
+ adapter.addAll(entries);
+ listWeatherView.setAdapter(adapter);
}
@Override
- public void onClickGetWeather(final View v) {
+ public void onClickGetWeather() {
final IJPOSWeatherParser JPOSWeatherParser = new JPOSWeatherParser();
final WeatherService weatherService = new WeatherService(
final WeatherTask weatherTask = new WeatherTask(HTTPweatherClient, weatherService);
- weatherTask.execute("London,uk");
+ weatherTask.execute("Candeleda,spain");
}
public void updateWeatherData(final WeatherData weatherData) {
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z");
final double tempUnits = this.isFahrenheit ? 0 : 273.15;
- if (weatherData.getWeather() != null) {
+ final List<WeatherDataEntry> entries = createEmptyEntriesList();
+
+ final ListView listWeatherView = (ListView) this.getActivity().findViewById(
+ R.id.weather_data_list_view);
+
+ final WeatherDataAdapter adapter = new WeatherDataAdapter(this.getActivity(),
+ R.layout.weather_data_entry_list);
+ if (weatherData.getWeather() != null) {
+ entries.set(0, new WeatherDataEntry(this.getString(R.string.text_field_description), weatherData.getWeather()
+ .getDescription()));
+ double conversion = weatherData.getMain().getTemp();
+ conversion = conversion - tempUnits;
+ entries.set(1, new WeatherDataEntry(this.getString(R.string.text_field_tem), tempFormatter.format(conversion)));
+ conversion = weatherData.getMain().getMaxTemp();
+ conversion = conversion - tempUnits;
+ entries.set(2, new WeatherDataEntry(this.getString(R.string.text_field_tem_max), tempFormatter.format(conversion)));
+ conversion = weatherData.getMain().getMinTemp();
+ conversion = conversion - tempUnits;
+ entries.set(3, new WeatherDataEntry(this.getString(R.string.text_field_tem_min), tempFormatter.format(conversion)));
}
if (weatherData.getSystem() != null) {
+ long unixTime = weatherData.getSystem().getSunRiseTime();
+ Date unixDate = new Date(unixTime * 1000L);
+ String dateFormatUnix = dateFormat.format(unixDate);
+ entries.set(4, new WeatherDataEntry(this.getString(R.string.text_field_sun_rise), dateFormatUnix));
+
+ unixTime = weatherData.getSystem().getSunSetTime();
+ unixDate = new Date(unixTime * 1000L);
+ dateFormatUnix = dateFormat.format(unixDate);
+ entries.set(5, new WeatherDataEntry(this.getString(R.string.text_field_sun_set), dateFormatUnix));
+ }
+ if (weatherData.getClouds() != null) {
+ final double cloudiness = weatherData.getClouds().getCloudiness();
+ entries.set(6, new WeatherDataEntry(this.getString(R.string.text_field_cloudiness), tempFormatter.format(cloudiness)));
}
if (weatherData.getIconData() != null) {
-
+ final Bitmap icon = BitmapFactory.decodeByteArray(
+ weatherData.getIconData(), 0,
+ weatherData.getIconData().length);
+ final ImageView imageIcon = (ImageView) getActivity().findViewById(R.id.weather_picture);
+ imageIcon.setImageBitmap(icon);
}
+
+
+
+ listWeatherView.setAdapter(null);
+ adapter.addAll(entries);
+ listWeatherView.setAdapter(adapter);
}
@Override
return weatherData;
}
}
+
+ private List<WeatherDataEntry> createEmptyEntriesList() {
+ final List<WeatherDataEntry> entries = new ArrayList<WeatherDataEntry>();
+ entries.add(new WeatherDataEntry(this.getString(R.string.text_field_description), null));
+ entries.add(new WeatherDataEntry(this.getString(R.string.text_field_tem), null));
+ entries.add(new WeatherDataEntry(this.getString(R.string.text_field_tem_max), null));
+ entries.add(new WeatherDataEntry(this.getString(R.string.text_field_tem_min), null));
+ entries.add(new WeatherDataEntry(this.getString(R.string.text_field_sun_rise), null));
+ entries.add(new WeatherDataEntry(this.getString(R.string.text_field_sun_set), null));
+ entries.add(new WeatherDataEntry(this.getString(R.string.text_field_cloudiness), null));
+ entries.add(new WeatherDataEntry(this.getString(R.string.text_field_rain_time), null));
+ entries.add(new WeatherDataEntry(this.getString(R.string.text_field_rain_amount), null));
+ entries.add(new WeatherDataEntry(this.getString(R.string.text_field_wind_speed), null));
+ entries.add(new WeatherDataEntry(this.getString(R.string.text_field_humidity), null));
+
+ return entries;
+ }
}