Update existing Preference-item in a PreferenceActivity upon returning from a (sub)PreferenceScreen

前端 未结 8 1826
清歌不尽
清歌不尽 2020-12-16 19:36

I have a PreferenceActivity with a bunch of (Sub)PreferenceScreens. Each such (Sub)PreferenceScreen represents an account and has the account-username as its title.

8条回答
  •  孤街浪徒
    2020-12-16 20:32

    notifyDataSetChanged() is right solution. But I want to add recursive iterator for all PreferenceScreens, as far as I got a problem to find real parent of a Preference. For complicated structure of Preferences I recommend this killer-code:

    private void updateAll_PrefereneScreens(PreferenceGroup group) {
        if (group instanceof PreferenceScreen) {
            BaseAdapter adapter = (BaseAdapter) ((PreferenceScreen) group).getRootAdapter();
            adapter.notifyDataSetChanged();
        }
        for (int i=0; i

    I call it after every setSummary() to ensure it works properly:

    findPreference("KEY").setSummary(str);
    updateAll_PrefereneScreens(getPreferenceScreen());
    

提交回复
热议问题