Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

android - Getting integer or index values from a list preference

I'm creating lists in a shared preference and when the onPreferenceChanged() method is called I want to extract the index of the item in the list or an integer value in some cases. I am trying to build the xml data as follows:

in the arrays:

<string-array name="BackgroundChoices">
  <item>Dark Background</item>
  <item>Light Background</item> 
</string-array>
<array name="BackgroundValues">
  <item>1</item>
  <item>0</item> 
</array>
<string-array name="SpeedChoices">
  <item>Slow</item>
  <item>Medium</item>
  <item>Fast</item>
</string-array>    
<array name="SpeedValues">
  <item>1</item>
  <item>4</item>
  <item>16</item>
</array>

in the preferences xml file:

<PreferenceScreen android:key="Settings"  
   xmlns:android="http://schemas.android.com/apk/res/android"  
   android:title="Settings">

<ListPreference
        android:key="keyBackground"
        android:entries="@array/BackgroundChoices"
        android:summary="Select a light or dark background." 
        android:title="Light or Dark Background"  
        android:positiveButtonText="Okay"   
        android:entryValues="@array/BackgroundValues"/>
<ListPreference 
        android:key="keySpeed" 
        android:entries="@array/SpeedChoices" 
        android:summary="Select animation speed."
        android:title="Speed" android:entryValues="@array/SpeedValues"/>
</PreferenceScreen>

So my xml does not work. I know how to do this using a string-array rather than an array for the values. And I could pull out the value strings and derive what I want from that but I would rather (if possible) be able to have lists where the values were ints, booleans, or enums. What is the customary way to do this?

thanks in advance,

Jay

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Put the preferences in as String and use Integer.parseInt(). I think there is actually a bug report on the limitation you are referring to but I can't find the link. From experience I can tell you to just use Strings and save your self a lot of frustration.

Note to other SO users, if you can prove me wrong, I welcome it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...