简体   繁体   中英

ListPreference does not display selected value

In my setting activity, after selecting a ListPreference to order the result list in ascending or descending, the selected vale does not appear in the ListPreference. Here is my code for this list preference. I have also attached a screenshot for this problem. Please let me know what is the problem and what was my mistake.

<ListPreference
        android:title="@string/pref_calllog_sorting"
        android:key="@string/pref_calllog_sorting_descending"
        android:defaultValue="@string/pref_calllog_sorting_descending"
        android:entryValues="@array/pref_calllog_sorting_values"
        android:entries="@array/pref_calllog_sorting_options" />

ListPreference 的屏幕截图

By putting special string %s into summary attribute, you tell Android to show the selected entry.

Your XML will then be

<ListPreference
        android:title="@string/pref_calllog_sorting"
        android:key="@string/pref_calllog_sorting_descending"
        android:defaultValue="@string/pref_calllog_sorting_descending"
        android:entryValues="@array/pref_calllog_sorting_values"
        android:entries="@array/pref_calllog_sorting_options"
        android:summary="%s"
        />

Need to call bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_calllog_sorting_descending))); in onPostCreate(Bundle savedInstanceState) method. bindPreferenceSymmaryToValue(Preference preference) method auto create when adding SettingActivity in application.

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    setupActionBar();
    bindPreferenceSummaryToValue(findPreference(
            getString(R.string.pref_calllog_sorting_descending)));
}

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(), ""));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM