1 package de.example.exampletdd.fragment.preferences;
3 import android.content.SharedPreferences;
4 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
5 import android.os.Bundle;
6 import android.preference.Preference;
7 import android.preference.PreferenceFragment;
8 import de.example.exampletdd.R;
10 public class WeatherInformationPreferencesFragment extends PreferenceFragment
11 implements OnSharedPreferenceChangeListener {
14 public void onCreate(final Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
17 // Load the preferences from an XML resource
18 this.addPreferencesFromResource(R.xml.weather_preferences);
20 String keyPreference = this.getActivity().getString(
21 R.string.weather_preferences_units_key);
22 Preference connectionPref = this.findPreference(keyPreference);
23 connectionPref.setSummary(this.getPreferenceManager()
24 .getSharedPreferences().getString(keyPreference, ""));
26 keyPreference = this.getActivity().getString(
27 R.string.weather_preferences_language_key);
28 connectionPref = this.findPreference(keyPreference);
29 connectionPref.setSummary(this.getPreferenceManager()
30 .getSharedPreferences().getString(keyPreference, ""));
32 keyPreference = this.getActivity().getString(
33 R.string.weather_preferences_day_forecast_key);
34 connectionPref = this.findPreference(keyPreference);
35 final String value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
36 String humanValue = "";
37 if (value.equals("5")) {
38 humanValue = "5-Day Forecast";
39 } else if (value.equals("10")) {
40 humanValue = "10-Day Forecast";
41 } else if (value.equals("14")) {
42 humanValue = "14-Day Forecast";
44 connectionPref.setSummary(humanValue);
48 public void onResume() {
50 this.getPreferenceManager().getSharedPreferences()
51 .registerOnSharedPreferenceChangeListener(this);
56 public void onPause() {
58 this.getPreferenceManager().getSharedPreferences()
59 .unregisterOnSharedPreferenceChangeListener(this);
63 public void onSharedPreferenceChanged(
64 final SharedPreferences sharedPreferences, final String key) {
65 String keyValue = this.getActivity().getString(
66 R.string.weather_preferences_units_key);
68 if (key.equals(keyValue)) {
69 final Preference connectionPref = this.findPreference(key);
70 connectionPref.setSummary(sharedPreferences.getString(key, ""));
74 keyValue = this.getActivity().getString(
75 R.string.weather_preferences_language_key);
76 if (key.equals(keyValue)) {
77 final Preference connectionPref = this.findPreference(key);
78 connectionPref.setSummary(sharedPreferences.getString(key, ""));
82 keyValue = this.getActivity().getString(
83 R.string.weather_preferences_day_forecast_key);
84 if (key.equals(keyValue)) {
85 final Preference connectionPref = this.findPreference(key);
86 final String value = sharedPreferences.getString(key, "");
87 String humanValue = "";
88 if (value.equals("5")) {
89 humanValue = "5-Day Forecast";
90 } else if (value.equals("10")) {
91 humanValue = "10-Day Forecast";
92 } else if (value.equals("14")) {
93 humanValue = "14-Day Forecast";
95 connectionPref.setSummary(humanValue);