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

后端 未结 3 870
北海茫月
北海茫月 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:52

    My understanding is that ListPreference can only work with string arrays. You'll have to use getString() and convert to integer yourself. See http://code.google.com/p/android/issues/detail?id=2096 for the bug report on this. It doesn't look like Google plans to extend ListPreference to handle anything but strings.

    Also: You'll need to store the preference as a string too. Otherwise, your preferences activity will crash when it starts up and tries to read a string value.

提交回复
热议问题