我有一个带有ListPreference的偏好活动,用于选择货币。 首选项的默认值为“CHF”。
但是,当我第一次安装应用程序并运行它时,就好像首选项没有存储在sharedPreferences文件中一样。
主要活动的文本视图取决于此首选项的值,因此即使第一次运行应用程序,我也需要绑定首选项。
在我的主要活动中,在onResume中,我调用以下方法:
private void bindMassageViews() {
String currency = PreferenceManager.getDefaultSharedPreferences(getContext())
.getString(getString(R.string.currency_preference_key), "");
massageName.setText(mBuilder.getMassageName());
priceContentTextview.setText(String.format(priceElement,
String.valueOf(mBuilder.getPrice()), currency));
}
偏好本身如下:
<ListPreference
android:defaultValue="@string/swiss_franc_symbol"
android:entries="@array/pref_currency_values"
android:entryValues="@array/pref_currency_values"
android:key="@string/currency_preference_key"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_currency" />
我的偏好活动是:
公共类SettingsActivity扩展了AppCompatPreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
bindPreferences();
setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
if (!super.onMenuItemSelected(featureId, item)) {
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
*/
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@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.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* @see #sBindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
private void bindPreferences() {
bindPreferenceSummaryToValue(findPreference(getString(R.string.currency_preference_key)));
}
}
首次运行应用时,如何确保首选项的默认值会被绑定?
答案 0 :(得分:1)
复制我的评论有助于解决问题:
尝试在onCreate中添加
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
,看看它是否有任何改变