Change the summary of a ListPreference with the new value (Android)

前端 未结 14 984
感动是毒
感动是毒 2020-12-30 03:01

How can I modify the summary of a ListPreference to the new \"Entry\" string selected by the user (not the entry value)

I suppouse its with setOnPreferenceChangeList

相关标签:
14条回答
  • 2020-12-30 03:42

    I know that it's a very old question, but it's still actual. To have the summary automatically updated you have to call the original preferenceChangeListener:

    final OnPreferenceChangeListener listener = preference.getOnPreferenceChangeListener();
    preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
            {
                @Override
                public boolean onPreferenceChange(Preference preference, Object o)
                {
                    if (listener !=null)
                        listener .onPreferenceChange(preference, o);
    
                    return true;
                }
            });
    
    0 讨论(0)
  • 2020-12-30 03:43

    I recommend to implement the OnSharedPreferenceChangeListener in your PreferenceFragment or PreferenceActivity instead of Preference.setOnPreferenceChangeListner. Use setSummay to set the new changes. (Do not forget to register and unregister the listener.) This listener is called after the change to the preference has been completed. You should also set a default value in the XML for the preferences.

    0 讨论(0)
  • 2020-12-30 03:43

    There is no need to extend ListPreference or to loop over the entryValues etc

    public boolean onPreferenceChange(Preference preference, Object newValue) {
        int i = ((ListPreference)preference).findIndexOfValue(newValue.toString());
        CharSequence[] entries = ((ListPreference)preference).getEntries();
        preference.setSummary(entries[i]);
    
        return true;
    }
    
    0 讨论(0)
  • 2020-12-30 03:44

    The "%s" solution works for me on android 4.0.3, by writting %s directly in the XML file. The problem is the text is not updated after I changed the value of the preference but it is when I modify another preference of my PreferenceScreen. Maybe some refresh is missing here.

    0 讨论(0)
  • 2020-12-30 03:45

    For all I know:

    a) The %s does work on android 4, but not on 2.x.

    b) The update is achieved if you set a dummy value in between, see here: https://stackoverflow.com/a/16397539/1854563

    0 讨论(0)
  • 2020-12-30 03:46

    Nauman Zubair is right.

    The %s implementation is buggy. The view shows the correct value on first load (if a default list value is set), but the view does not update when selecting a list item. You have to toggle a checkbox or some other preference to update the view.

    As a workaround, implement OnSharedPreferenceChangeListener to override the summary for the list preference.

    /src/apps/app_settings/SettingsActivity.java

    package apps.app_settings;
    
    import android.os.Bundle;
    import android.preference.PreferenceActivity;
    
    public class SettingsActivity extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            /* set fragment */
            getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
        }
    }
    

    /src/apps/app_settings/SettingsFragment.java

    package apps.app_settings;
    
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
    import android.os.Bundle;
    import android.preference.ListPreference;
    import android.preference.Preference;
    import android.preference.PreferenceFragment;
    
    public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            /* set preferences */
            addPreferencesFromResource(R.xml.preferences);
        }
    
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            /* get preference */
            Preference preference = findPreference(key);
    
            /* update summary */
            if (key.equals("list_0")) {
                preference.setSummary(((ListPreference) preference).getEntry());
            }
        }
    
        @Override
        public void onResume() {
            super.onResume();
            getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
        }
    
        @Override
        public void onPause() {
            getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
            super.onPause();
        }
    }
    

    /res/xml/preferences.xml

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen
      xmlns:android="http://schemas.android.com/apk/res/android">
      <ListPreference
        android:key="list_0"
        android:title="@string/settings_list_0_title"
        android:summary="%s"
        android:entries="@array/settings_list_0_entries"
        android:entryValues="@array/settings_list_0_entry_values"
        android:defaultValue="@string/settings_list_0_default_value"/>
    </PreferenceScreen>
    

    /res/values/strings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <string name="settings_list_0_title">list 0</string>
      <string-array name="settings_list_0_entries">
        <item>item 0</item>
        <item>item 1</item>
        <item>item 2</item>
      </string-array>
      <string-array name="settings_list_0_entry_values">
        <item>0</item>
        <item>1</item>
        <item>2</item>
      </string-array>
      <string name="settings_list_0_default_value">0</string>
    </resources>
    
    0 讨论(0)
提交回复
热议问题