Thiha Zaw
Thiha Zaw

Reputation: 826

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" />

Screenshot for ListPreference

Upvotes: 5

Views: 2475

Answers (3)

Kris Bogaert
Kris Bogaert

Reputation: 21

It is sufficient to add app:useSimpleSummaryProvider="true"

        <ListPreference
            app:key="myKey"
            app:title="@string/line_type"
            app:defaultValue="line"
            app:entries="@array/type_entries"
            app:entryValues="@array/type_values"
            app:useSimpleSummaryProvider="true"/>

Upvotes: 1

Thiha Zaw
Thiha Zaw

Reputation: 826

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

Upvotes: 2

Štěp&#225;n
Štěp&#225;n

Reputation: 348

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"
        />

Upvotes: 18

Related Questions