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

5

18

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

<ListPreference android:key="@string/prefGestureAccuracyKey"
    android:title="@string/prefGestureAccuracyTitle" android:summary="@string/prefGestureAccuracyDesc"
    android:entries="@array/prefNumberAccuracyLabels" android:entryValues="@array/prefNumberAccuracyValues"
    android:dialogTitle="@string/prefGestureAccuracyDialog"
    android:persistent="true" android:defaultValue="2"
    android:shouldDisableView="false" />

And I want to get the value with something like: int val = sharedPrefs.getInt(key, defaultValue).

At the moment I have to use getString and parse the result.

Corinacorine answered 24/4, 2010 at 17:2 Comment(3)
That should work according to the manual developer.android.com/intl/de/reference/android/content/…Barbarity
I don't get your question. Which value are you willing to get?Madness
Well that Xml creates a preference whose type is String not Integer. I would like a Preference whose value is strongly typed as an int. This question arose from a previous one I wrote which explains the context: #2705591Corinacorine
S
40

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.

Siloxane answered 13/6, 2010 at 3:14 Comment(1)
Thanks. I hadn't come across that bug report.Corinacorine
A
6

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 :)

Ankerite answered 28/9, 2011 at 18:21 Comment(0)
S
2

In fact as you can read in the docs:

http://developer.android.com/reference/android/preference/ListPreference.html#getValue()

The method to get a ListPreference value is:

public String getValue ()

So you get a String. It's not a big deal, but could be prettier to admit integers.

Sinistral answered 16/5, 2012 at 21:35 Comment(0)
S
0

You need to create an xml resource and set your default value to that. Anything you enter in the XML file as a literal is treated as a string, so the ListPreference will throw a null pointer exception when it tries to find a string in an integer array.

Sis answered 4/5, 2012 at 16:42 Comment(0)
W
0

But for me, you first get a string type by: String numStr = prefs.getString("key", "-555"); then: `

try {
    int yourInt =  Integer.parseInt(numStr);
    if(yourInt != -555)
    {
        //do your stuff
    }
}
catch(Exception e)
{
    ...
}

`

Willena answered 11/8, 2016 at 0:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.