如何获取在 Xml 中定义的值为整数的 Android ListPreference?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2705347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:01:25  来源:igfitidea点击:

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

androidandroid-preferences

提问by Rob Kent

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

是否可以在 Xml 中定义 ListPreference 并使用 getInt 从 SharedPreferences 检索值?这是我的 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).

我想通过以下方式获取值:int val = sharedPrefs.getInt(key, defaultValue)。

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

目前我必须使用 getString 并解析结果。

回答by Edward Falk

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=2096for the bug report on this. It doesn't look like Google plans to extend ListPreference to handle anything but strings.

我的理解是 ListPreference 只能处理字符串数组。您必须使用 getString() 并自己转换为整数。有关此问题的错误报告,请参阅http://code.google.com/p/android/issues/detail?id=2096。看起来 Google 不打算扩展 ListPreference 来处理除字符串之外的任何内容。

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.

另外:您还需要将首选项存储为字符串。否则,您的首选项活动将在启动并尝试读取字符串值时崩溃。

回答by Michael

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().

我使用了 ListPreference 并使我的 entryValues 数组包含可以使用 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.

然后在我的 PreferencesActivity 中,我为此首选项设置了一个 OnPreferenceChangeListener,在 onPreferenceChange() 方法中,我为整数版本设置了一个不同的首选项 - 第二个是我在代码中实际使用的首选项。第一个仅用于用户选项。

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

这样我就不必每次需要查看 String 时将其转换为 int,我只需在用户设置它时执行。也许矫枉过正,但它确实有效:)

回答by viridis

In fact as you can read in the docs:

事实上,正如您在文档中所读到的:

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

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

The method to get a ListPreferencevalue is:

获取ListPreference值的方法是:

public String getValue ()

public String getValue ()

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

所以你得到一个字符串。这没什么大不了的,但承认整数可能会更好。

回答by Ahmad Tukur

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

但对我来说,你首先通过以下方式获得字符串类型: String numStr = prefs.getString("key", "-555"); 然后:`

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

`

`

回答by Dewey

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 ListPreferencewill throw a null pointer exception when it tries to find a string in an integer array.

您需要创建一个 xml 资源并将默认值设置为该资源。您在 XML 文件中作为文字输入的任何内容都被视为字符串,因此ListPreference在尝试在整数数组中查找字符串时将抛出空指针异常。