Android ListPreference项将作为索引而不是字符串值进行检索
我正在尝试做一个应用程序,它将预测未来7天的天气数据。我正在添加一个设置活动,用户可以在其中将温度单位更改为华氏或公制 我将其定义为一个ListPreference,其条目取自一个数组,其值为metric和Fahrenheit,如下所示:Android ListPreference项将作为索引而不是字符串值进行检索,android,android-activity,sharedpreferences,application-settings,listpreference,Android,Android Activity,Sharedpreferences,Application Settings,Listpreference,我正在尝试做一个应用程序,它将预测未来7天的天气数据。我正在添加一个设置活动,用户可以在其中将温度单位更改为华氏或公制 我将其定义为一个ListPreference,其条目取自一个数组,其值为metric和Fahrenheit,如下所示: <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" a
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<!--location preference:-->
<EditTextPreference
android:key="@string/pref_location_key"
android:title="@string/locTitle"
android:defaultValue="@string/pref_location_default"
android:inputType="text"
android:singleLine="true"
android:summary="enter location"/>
<!--temperature unit preference:-->
<ListPreference android:key="@string/pref_units_key"
android:defaultValue="@string/pref_units_metric"
android:title="Temperature Unit"
android:summary="select a temperature unit"
android:entries="@array/units"
android:entryValues="@array/indx"/>
</PreferenceScreen>
pref_units_imperial包含字符串Fahrenheit,但除非将其写入
if (unitType.equalsIgnoreCase ("1")), unitType has been fetched from the shared preferences previously and sent to the method.
这是我使用的设置类
public class SettingsActivity extends PreferenceActivity
implements Preference.OnPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add 'general' preferences, defined in the XML file
// TODO: Add preferences from XML
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
bindPreferenceSummaryToValue(findPreference (getString(R.string.pref_units_key))); //this will find the preference value associated with locKey key and pass it to be saved in the shared preferences
//we are just getting a reference to the preference, we are not fetching any data from it.
}
private void bindPreferenceSummaryToValue(Preference preference) { //receives user preference
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(this);
// Trigger the listener immediately with the preference's
// current value.
onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext()) //conetxt from which this is called. (returns all shared preferences in the app, so you need to distinguish
.getString(preference.getKey(), "")); //the key of the setting that has been changed
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]); //get the entries we have specified in the array xml by the indices associated with them
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
return true;
}
}
在这行preference.setSummary(listPreference.getEntries()[prefIndex])中,它应该得到华氏温度或公制,但它似乎得到了“0”或“1”,我想得到单位的字符串名称,我试着调试应用程序,但我不知道为什么它要分配索引值而不是条目
谁能帮我解决这个问题吗
感谢您的帮助
谢谢。您解决了这个问题吗?如果没有,您是否只是尝试添加字符串值(不是作为strings.xml的引用)作为输入值?@dania,您找到解决方案了吗?
if (unitType.equalsIgnoreCase ("1")), unitType has been fetched from the shared preferences previously and sent to the method.
public class SettingsActivity extends PreferenceActivity
implements Preference.OnPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add 'general' preferences, defined in the XML file
// TODO: Add preferences from XML
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
bindPreferenceSummaryToValue(findPreference (getString(R.string.pref_units_key))); //this will find the preference value associated with locKey key and pass it to be saved in the shared preferences
//we are just getting a reference to the preference, we are not fetching any data from it.
}
private void bindPreferenceSummaryToValue(Preference preference) { //receives user preference
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(this);
// Trigger the listener immediately with the preference's
// current value.
onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext()) //conetxt from which this is called. (returns all shared preferences in the app, so you need to distinguish
.getString(preference.getKey(), "")); //the key of the setting that has been changed
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]); //get the entries we have specified in the array xml by the indices associated with them
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
return true;
}
}