How to update summary of preference from sub-prefe

2019-08-31 08:50发布

问题:

I can't solve this problem. I have preference screen and there is sub-preference that opens up another screen. On that another screen change of items can be caught with OnSharedPreferenceChangeListener and I change summary in parent preference screen, but when I go back to that parent preference screen, summary did not changed.

Same question was asked here, but conclusion was not clear, and I could not solve this problem. It seems a common problem to me and I guess there is good solution for this.

Dose anyone know a solution for this problem?

  • There is one thing I like to keep: sub-preference is standard one, not custom.

回答1:

I've solved this by adding OnPreferenceClickListener to the preferences which will change the summary in the main screen.

OnPreferenceClickListener viewUpdater = new OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            updateView();
            return  false;
        }
    };

Within the updateView() method I'm setting the summary to a new value and then I'm using the invalidateViews method of the preferences listview to trigger an update of the displayed summary

private void updateView() {
    preference.setSummary(newSummary);
    getListView().invalidateViews();
}


回答2:

Check the answer of @jmbouffard that's work for me