set summaries for list preferences
authorgitknilch <gitknilch@cwde.de>
Tue, 26 Feb 2013 09:49:28 +0000 (10:49 +0100)
committergitknilch <gitknilch@cwde.de>
Tue, 26 Feb 2013 09:49:28 +0000 (10:49 +0100)
src/de/cwde/shisensho/SettingsActivity.java

index f2ceaa2..275638e 100644 (file)
@@ -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);
+               }
+       }       
 }
Impressum, Datenschutz