ListPreference 将值存储为字符串而不是整数

ListPreference Storing Values as Strings Not Ints

我无法理解如何将 ListPreference 的条目值保存为整数。截至目前,我已将 ListPreference 定义如下:

    <ListPreference
    android:defaultValue="@string/list_preference_sorting_options_default_value"
    android:title="@string/list_preference_sorting_options_title"
    android:key="@string/list_preference_sorting_options_key"
    android:entries="@array/list_preference_sorting_options_entries"
    android:entryValues="@array/list_preference_sorting_options_entry_values"/>

...其中 entriesentryValues 属性在以下数组中定义:

<array name="list_preference_sorting_options_entries">
    <item>@string/list_preference_sorting_options_entry_popularity</item>
    <item>@string/list_preference_sorting_options_entry_top_rated</item>
</array>

<array name="list_preference_sorting_options_entry_values">
    <item>@string/list_preference_sorting_options_entry_value_popularity</item>
    <item>@string/list_preference_sorting_options_entry_value_top_rated</item>
</array>

我知道我在 list_preference_sorting_options_entry_values 数组中使用字符串值。但是,如果我以不同的方式定义我的数组,例如:

<array name="list_preference_sorting_options_entry_values">
    <item>0</item>
    <item>1</item>
</array>

...然后在我的应用程序中,如果我尝试访问设置 activity,我的应用程序就会崩溃。

此外,如果我尝试从 SharedPreferences 中读取我的条目值作为整数(即​​使它们存储为字符串)如下:

int methodFlag = preferences.getInt(getString(R.string.list_preference_sorting_options_key), 0);

...然后我收到一个 Java 错误,我无法将 String 转换为 int。为了正确获取整型的入口值,我需要使用getString()方法,然后解析成整型:

    String preferenceMethodFlagString = preferences.getString(getString(R.string.list_preference_sorting_options_key),getString(R.string.list_preference_sorting_options_default_value));

    int preferenceMethodFlag = Integer.parseInt(preferenceMethodFlagString);

有没有办法让我通过arrays.xml直接存储整数值?如果我使用此实现(使用 arrays.xml),我是否总是必须将 String 解析为 int?

我见过其他 SO 问题,如果使用 SharedPreferences.Editor,整数值将存储到 ListPreference 的条目值中。这是存储整数值的唯一方法吗?

你做不到。 Android 仅对条目和条目值使用字符串数组。但是您可以使用 Integer.parseInt() 方法轻松地将 String 转换为 int。希望这有帮助。

ListPreference 的设计使其只能保存字符串值。

ListPreference的成员变量如下

private CharSequence[] mEntries;
private CharSequence[] mEntryValues;
private String mValue;
private String mSummary;

此外,值被读取检索为

mEntries = a.getTextArray(com.android.internal.R.styleable.ListPreference_entries);
mEntryValues = a.getTextArray(com.android.internal.R.styleable.ListPreference_entryValues);

getTextArray() 仅查找字符串数组资源 ID。

因此,条目和条目值应该始终是字符串资源。

如果你想使用 int 值,

<string-array name="entries_list_preference">
    <item>0</item> <!-- This does not make it int, it will be stored as string only -->
    <item>1</item>
    <item>2</item>
</string-array>

<string-array name="entryvalues_list_preference">
    <item>0</item>
    <item>1</item>
    <item>2</item>
</string-array>

当您读取它的值时,将其解析为整数。

ListPreference listPreference = (ListPreference) findPreference ("list_preference");
String value = listPreference.getValue();
if(!TextUtils.isEmpty(value))
    performAction(Integer.parseInt(listPreference.getValue()));

现在您可以根据自己的需要编写 performAction(int type) 方法了。