From: gitknilch Date: Tue, 26 Feb 2013 09:49:28 +0000 (+0100) Subject: set summaries for list preferences X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/FreeShisen/commitdiff_plain/a320f5ea5e433872fe474a34e25ce96e396a3b95?hp=29a0130122c0a33e33b12cfff5bc89e18168b738 set summaries for list preferences --- diff --git a/src/de/cwde/shisensho/SettingsActivity.java b/src/de/cwde/shisensho/SettingsActivity.java index f2ceaa2..275638e 100644 --- a/src/de/cwde/shisensho/SettingsActivity.java +++ b/src/de/cwde/shisensho/SettingsActivity.java @@ -2,11 +2,12 @@ package de.cwde.shisensho; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import android.content.res.Resources; import android.os.Bundle; import android.preference.*; public class SettingsActivity extends PreferenceActivity - implements OnSharedPreferenceChangeListener { + implements OnSharedPreferenceChangeListener { private ShisenSho app; @@ -17,11 +18,16 @@ public class SettingsActivity extends PreferenceActivity @SuppressWarnings("deprecation") @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - app = ShisenSho.app(); - addPreferencesFromResource(R.xml.preferences); - } + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + app = ShisenSho.app(); + addPreferencesFromResource(R.xml.preferences); + SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences(); + + sharedPreferences.registerOnSharedPreferenceChangeListener(this); + updateSummary(sharedPreferences, KEY_PREF_DIFF, KEY_PREF_DIFF, R.array.difficulties); + updateSummary(sharedPreferences, KEY_PREF_SIZE, KEY_PREF_SIZE, R.array.sizes); + } @Override public void onBackPressed() { @@ -32,32 +38,37 @@ public class SettingsActivity extends PreferenceActivity @SuppressWarnings("deprecation") @Override protected void onResume() { - super.onResume(); - getPreferenceScreen().getSharedPreferences() - .registerOnSharedPreferenceChangeListener(this); + super.onResume(); + getPreferenceScreen().getSharedPreferences() + .registerOnSharedPreferenceChangeListener(this); } @SuppressWarnings("deprecation") @Override protected void onPause() { - super.onPause(); - getPreferenceScreen().getSharedPreferences() - .unregisterOnSharedPreferenceChangeListener(this); + super.onPause(); + getPreferenceScreen().getSharedPreferences() + .unregisterOnSharedPreferenceChangeListener(this); } - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (key.equals(KEY_PREF_DIFF)) { - @SuppressWarnings("deprecation") - Preference myPref = findPreference(key); - // Set summary to be the user-description for the selected value - myPref.setSummary(sharedPreferences.getString(key, "")); - } - if (key.equals(KEY_PREF_SIZE)) { - @SuppressWarnings("deprecation") - Preference myPref = findPreference(key); - // Set summary to be the user-description for the selected value - myPref.setSummary(sharedPreferences.getString(key, "")); - } - } + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + updateSummary(sharedPreferences, key, KEY_PREF_DIFF, R.array.difficulties); + updateSummary(sharedPreferences, key, KEY_PREF_SIZE, R.array.sizes); + } + + private void updateSummary(SharedPreferences sharedPreferences, String changedkey, String mykey, int myresource) { + if (changedkey.equals(mykey)) { + // FIXME: handle NumberFormatException here? + int i = Integer.parseInt(sharedPreferences.getString(changedkey, "1")); + + Resources res = getResources(); + String[] mystrings = res.getStringArray(myresource); + String name = mystrings[i-1]; + + @SuppressWarnings("deprecation") + Preference myPref = findPreference(changedkey); + myPref.setSummary("Currently: " + name); + } + } }