4dc70d691fb7edd85229ab97043eab7943e48440
[JavaForFun] /
1 package de.example.exampletdd.fragment.preferences;
2
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;
9
10 public class WeatherInformationPreferencesFragment extends PreferenceFragment
11 implements OnSharedPreferenceChangeListener {
12
13     @Override
14     public void onCreate(final Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16
17         // Load the preferences from an XML resource
18         this.addPreferencesFromResource(R.xml.weather_preferences);
19
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, ""));
25
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, ""));
31
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";
43         }
44         connectionPref.setSummary(humanValue);
45     }
46
47     @Override
48     public void onResume() {
49         super.onResume();
50         this.getPreferenceManager().getSharedPreferences()
51         .registerOnSharedPreferenceChangeListener(this);
52
53     }
54
55     @Override
56     public void onPause() {
57         super.onPause();
58         this.getPreferenceManager().getSharedPreferences()
59         .unregisterOnSharedPreferenceChangeListener(this);
60     }
61
62     @Override
63     public void onSharedPreferenceChanged(
64             final SharedPreferences sharedPreferences, final String key) {
65         String keyValue = this.getActivity().getString(
66                 R.string.weather_preferences_units_key);
67
68         if (key.equals(keyValue)) {
69             final Preference connectionPref = this.findPreference(key);
70             connectionPref.setSummary(sharedPreferences.getString(key, ""));
71             return;
72         }
73
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, ""));
79             return;
80         }
81
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";
94             }
95             connectionPref.setSummary(humanValue);
96             return;
97         }
98
99     }
100
101 }