How to get an Android ListPreference defined in Xml whose values are integers?

后端 未结 3 937
北海茫月
北海茫月 2021-02-12 17:40

Is it possible to define a ListPreference in Xml and retrieve the value from SharedPreferences using getInt? Here is my Xml:



        
3条回答
  •  半阙折子戏
    2021-02-12 17:54

    I did this in a little more extreme way.

    I used the ListPreference and made my entryValues array contain Strings that I can convert to integer with Integer.parseInt().

    Then in my PreferencesActivity, I setup a OnPreferenceChangeListener for this preference, and in the onPreferenceChange() method I set a different preference to the integer version - this second one is the one I actually use in my code. The first is there just for the user option.

    This way I don't have to convert a String to int each time I need to look at it, I just do it when the user sets it. Perhaps overkill, but it does work :)

提交回复
热议问题