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_update_time_rate_key);
28 connectionPref = this.findPreference(keyPreference);
29 String value = this.getPreferenceManager().getSharedPreferences()
30 .getString(keyPreference, "");
31 String humanValue = "";
32 if (value.equals("60")) {
33 humanValue = "1 minute";
34 } else if (value.equals("120")) {
35 humanValue = "2 minutes";
36 } else if (value.equals("300")) {
37 humanValue = "5 minutes";
38 } else if (value.equals("600")) {
39 humanValue = "10 minutes";
40 } else if (value.equals("900")) {
41 humanValue = "15 minutes";
43 connectionPref.setSummary(humanValue);
45 keyPreference = this.getActivity().getString(
46 R.string.weather_preferences_day_forecast_key);
47 connectionPref = this.findPreference(keyPreference);
48 value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
50 if (value.equals("5")) {
51 humanValue = "5-Day Forecast";
52 } else if (value.equals("10")) {
53 humanValue = "10-Day Forecast";
54 } else if (value.equals("14")) {
55 humanValue = "14-Day Forecast";
57 connectionPref.setSummary(humanValue);
61 public void onResume() {
63 this.getPreferenceManager().getSharedPreferences()
64 .registerOnSharedPreferenceChangeListener(this);
69 public void onPause() {
71 this.getPreferenceManager().getSharedPreferences()
72 .unregisterOnSharedPreferenceChangeListener(this);
76 public void onSharedPreferenceChanged(
77 final SharedPreferences sharedPreferences, final String key) {
78 String keyValue = this.getActivity().getString(
79 R.string.weather_preferences_units_key);
81 if (key.equals(keyValue)) {
82 final Preference connectionPref = this.findPreference(key);
83 connectionPref.setSummary(sharedPreferences.getString(key, ""));
87 keyValue = this.getActivity().getString(R.string.weather_preferences_update_time_rate_key);
88 if (key.equals(keyValue)) {
89 final Preference connectionPref = this.findPreference(key);
90 final String value = sharedPreferences.getString(key, "");
91 String humanValue = "";
92 if (value.equals("60")) {
93 humanValue = "1 minute";
94 } else if (value.equals("120")) {
95 humanValue = "2 minutes";
96 } else if (value.equals("300")) {
97 humanValue = "5 minutes";
98 } else if (value.equals("600")) {
99 humanValue = "10 minutes";
100 } else if (value.equals("900")) {
101 humanValue = "15 minutes";
103 connectionPref.setSummary(humanValue);
107 keyValue = this.getActivity().getString(
108 R.string.weather_preferences_day_forecast_key);
109 if (key.equals(keyValue)) {
110 final Preference connectionPref = this.findPreference(key);
111 final String value = sharedPreferences.getString(key, "");
112 String humanValue = "";
113 if (value.equals("5")) {
114 humanValue = "5-Day Forecast";
115 } else if (value.equals("10")) {
116 humanValue = "10-Day Forecast";
117 } else if (value.equals("14")) {
118 humanValue = "14-Day Forecast";
120 connectionPref.setSummary(humanValue);