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

Java ListPreference将值存储为字符串而不是整数,java,android,android-sharedpreferences,Java,Android,Android Sharedpreferences,我很难理解如何将ListPreference的条目值保存为整数。截至目前,我已将我的ListPreference定义如下: <ListPreference android:defaultValue="@string/list_preference_sorting_options_default_value" android:title="@string/list_preference_sorting_options_title" android:key="@

我很难理解如何将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"/>
int methodFlag = preferences.getInt(getString(R.string.list_preference_sorting_options_key), 0);
我知道我在列表\首选项\排序\选项\条目\值数组中使用字符串值。但是,如果要以不同的方式定义数组,例如:

<array name="list_preference_sorting_options_entry_values">
    <item>0</item>
    <item>1</item>
</array>
…然后我收到一个Java错误,无法将字符串转换为int。为了正确获取作为int的条目值,我需要使用getString方法,然后将其解析为int:

    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一起使用,是否总是需要将字符串解析为int


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

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

你做不到。Android只对条目和条目值使用字符串数组。但是,您可以使用Integer.parseInt方法轻松地将字符串转换为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>

现在,您可以根据需要编写PerformAction类型方法。

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>

现在,您可以根据需要编写PerformAction类型方法。

尚未收到对此的响应。是否有人对此有任何输入?如果您将输入值放在整数数组而不是数组中会发生什么?哦,您不可能还没有收到对此的响应。有人会对此有任何输入吗?如果您将输入值放在整数数组而不是数组中会发生什么?哦,您不能