Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 851
jbww Avatar asked Mar 08 '11 01:03

jbww


3 Answers

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.

like image 172
Andrew White Avatar answered Nov 06 '22 01:11

Andrew White


Andrew was correct, the thread is here:

it's still being commented on, and yet no change (as of 2.3.3 anyway).

Integer.parseInt() of .valueOf() will have to work. If valueOf() works without error, use it, as it doesn't allocate as much as parseInt() does, helpful when you NEED to avoid GC like I do.

like image 10
bclymer Avatar answered Nov 06 '22 01:11

bclymer


Based on the Android's ListPreference, I created IntListPreference. The usage is straighforward - simply put this snippet in your preferences xml:

<org.bogus.android.IntListPreference
    android:key="limitCacheLogs"
    android:defaultValue="20"
    android:entries="@array/pref_download_logs_count_titles"
    android:entryValues="@array/pref_download_logs_count_values"
    android:negativeButtonText="@null"
    android:positiveButtonText="@null"
    android:title="@string/pref_download_logs_count" 
/>    

and that in strings.xml

<string name="pref_download_logs_count">Number of logs per cache</string>
<string-array name="pref_download_logs_count_titles">
    <item>10</item>
    <item>20</item>
    <item>50</item>
    <item>Give me all!</item>
</string-array>
<integer-array name="pref_download_logs_count_values">
    <item>10</item>
    <item>20</item>
    <item>50</item>
    <item>-1</item>
</integer-array>
like image 5
Boguś Avatar answered Nov 06 '22 02:11

Boguś