Default bindPreferenceSummaryToValue crashes for non-string types

前端 未结 2 1484
后悔当初
后悔当初 2021-02-13 13:26

I\'m following the example method to add a compatible preference/ fragment dialog found here. When doing so, I have found that if I have preferences that are Integers, Boolean,

相关标签:
2条回答
  • 2021-02-13 14:18

    There isn't a clean solution that I can find, but the best workaround seems to be something like this, using instanceof to figure out what type of data you are dealing with.

    private static void bindPreferenceSummaryToValue(Preference preference) {
        // Set the listener to watch for value changes.
        preference
                .setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
    
    
        if (preference instanceof SeekBarPreference)
        {
            // Trigger the listener immediately with the preference's
            // current value.
            sBindPreferenceSummaryToValueListener.onPreferenceChange(
                    preference,
                    PreferenceManager.getDefaultSharedPreferences(
                            preference.getContext()).getInt(preference.getKey(),0));
        }
        else if (preference instanceof CheckBoxPreference)
        {
            // Trigger the listener immediately with the preference's
            // current value.
            sBindPreferenceSummaryToValueListener.onPreferenceChange(
                    preference,
                    PreferenceManager.getDefaultSharedPreferences(
                            preference.getContext()).getBoolean(preference.getKey(),true));
        }
    }
    
    0 讨论(0)
  • 2021-02-13 14:28
    private static void bindPreferenceSummaryToValue(Preference preference) {
    //Todo
    }
    

    this method is used for showing the current value of your preference, e.g If you'r using <SwitchPreference/> which has value as true or false it will display that value in summary. You do not need to show boolean values as summary. This method is used for <ListPreference/> or <RingtonePreference/> where you would like to see current value. Summary in this case is string but for <SwitchPreference/> it's boolean. Either you can ignore these boolean summary or can using the instanceof you can avoid exception.

    0 讨论(0)
提交回复
热议问题