Android ListPreference:使用字符串数组作为条目和整数数组作为条目值不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11346916/
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
ListPreference: use string-array as Entry and integer-array as Entry Values doesn't work
提问by Santacrab
I'm using in a settings.xml file a ListPreference. I want to show the user a list of 3 options to select. When the user chooses one of the options in the Settings, I get this error:
我在 settings.xml 文件中使用了 ListPreference。我想向用户显示要选择的 3 个选项的列表。当用户选择设置中的选项之一时,我收到此错误:
java.lang.NullPointerException
at android.preference.ListPreference.onDialogClosed(ListPreference.java:264)
at android.preference.DialogPreference.onDismiss(DialogPreference.java:381)
at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
This is the code of the ListPreference:
这是 ListPreference 的代码:
<ListPreference
android:entries="@array/date_alignement"
android:entryValues="@array/date_alignement_values"
android:key="settings_date_alignement"
android:summary="@string/settings_date_alignement_summary"
android:title="@string/settings_date_alignement_title" />
And here are the arrays I use to populate the entries:
这是我用来填充条目的数组:
<string-array name="date_alignement">
<item>"Top"</item>
<item>"Center"</item>
<item>"Bottom"</item>
</string-array>
<integer-array name="date_alignement_values">
<item >0</item>
<item >1</item>
<item >2</item>
</integer-array>
In my onSharedPreferenceChanged I use those values in this way:
在我的 onSharedPreferenceChanged 中,我以这种方式使用这些值:
@Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
//Text
mAlignment = mPrefs.getInt(PREF_ALIGNMENT, 1);
switch (mAlignment) {
case 0:
offsetY = mHeight/3.0f;
break;
case 2:
offsetY = mHeight*2.0f/3.0f;
break;
default:
offsetY = mHeight/2.0f;
break;
}
}
If I use another string-array for the entryValuesit works. For example if I use the same string array as values and entries:
如果我为entryValues使用另一个字符串数组,它会起作用。例如,如果我使用相同的字符串数组作为值和条目:
android:entries="@array/date_alignement"
android:entryValues="@array/date_alignement"
then I have to change my code a little but it works:
然后我必须稍微更改我的代码,但它有效:
if(mAlignment.equalsIgnoreCase("center")) {
offsetY = mHeight/2.0f;
} else if(mAlignment.equalsIgnoreCase("top")) {
offsetY = mHeight/3.0f;
} else if(mAlignment.equalsIgnoreCase("bottom")) {
offsetY = mHeight*2.0f/3.0f;
}
Why I can't use a string-array and an integer-array for a ListPreference entries and values?
为什么我不能对 ListPreference 条目和值使用字符串数组和整数数组?
回答by Egor
The answer is simple: because the Android is designed this way. It just uses String
arrays for both entries and entry values and that's all. And I can't see any problem in this fact, since you can easily convert a String
to an int
using the Integer.parseInt()
method. Hope this helps.
答案很简单:因为 Android 就是这样设计的。它只是将String
数组用于条目和条目值,仅此而已。而且我看不出有任何问题,因为您可以使用该方法轻松地将 a 转换String
为 an 。希望这可以帮助。int
Integer.parseInt()
回答by kasvith
The answer is listed in List Preference Documentation.
答案列在List Preference Documentation 中。
int findIndexOfValue (String value)
will return the index for given value and the argument is taken as a String, so the entryValues array should be a string array to get this work.
将返回给定值的索引,并且参数被视为字符串,因此 entryValues 数组应该是一个字符串数组才能完成这项工作。
回答by Sam
You can convert your entry values to strings to keep ListPreference
happy and then convert them to int
s when talking to the persistent data store.
您可以将条目值转换为字符串以保持ListPreference
满意,然后int
在与持久数据存储交谈时将它们转换为s。
- When setting the entry values, use strings instead of ints:
"1", "2", "3"
etc - Make a custom
IntListPreference
that persists the values as ints - In your
preferences.xml
file, change the<ListPreference>
into a<your.app.package.IntListPreference>
- 设置条目值时,使用字符串而不是整数:
"1", "2", "3"
等 - 自定义
IntListPreference
将值保留为整数 - 在你的
preferences.xml
文件,更改<ListPreference>
成<your.app.package.IntListPreference>
IntListPreference.java
IntListPreference.java
Here's a sample implementation. Tested and working on AndroidX Preference 1.0.0.
这是一个示例实现。在 AndroidX Preference 1.0.0 上测试和工作。
public class IntListPreference extends ListPreference {
public IntListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public IntListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public IntListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public IntListPreference(Context context) {
super(context);
}
@Override
protected boolean persistString(String value) {
int intValue = Integer.parseInt(value);
return persistInt(intValue);
}
@Override
protected String getPersistedString(String defaultReturnValue) {
int intValue;
if (defaultReturnValue != null) {
int intDefaultReturnValue = Integer.parseInt(defaultReturnValue);
intValue = getPersistedInt(intDefaultReturnValue);
} else {
// We haven't been given a default return value, but we need to specify one when retrieving the value
if (getPersistedInt(0) == getPersistedInt(1)) {
// The default value is being ignored, so we're good to go
intValue = getPersistedInt(0);
} else {
throw new IllegalArgumentException("Cannot get an int without a default return value");
}
}
return Integer.toString(intValue);
}
}
回答by Александр
The following worked for me:
以下对我有用:
String objectName = prefs.getString("listPrefMelodyYd1", "");
switch (objectName.toUpperCase()) {
case "1":
playSound(catSound);
break;
case "2":
playSound(dogSound);
break;
case "3":
playSound(cowSound);
break;
}