How do you refresh PreferenceActivity to show changes in the settings?

前端 未结 10 1774
情深已故
情深已故 2020-12-14 15:52

Based on the following code, can you tell me how to refresh the PreferenceActivity window to show changes in the settings immediately? For example: the user taps the master

10条回答
  •  温柔的废话
    2020-12-14 16:27

    After fighting this for a day, I figured out this.
    This is for multiple levels of preferences.

    parent is the preference screen that holds the preference you are trying to update.

    Since the settings screen displays as a dialog, you can get the listview from the dialog, then tell it's adapter to update child.

    PreferenceScreen parent  // The screen holding the child
    PreferenceScreen child   // The entry changing
    
    child.setSummary(isEnabled?"Enabled":"Not Enabled");
    ListView v = (ListView)parent.getDialog().findViewById(android.R.id.list);
    BaseAdapter ba = (BaseAdapter)v.getAdapter();
    ba.notifyDataSetChanged();
    

    I found R.id.list is not available on older phones but this works

        ListAdapter adapter = parent.getRootAdapter();
        if (adapter instanceof BaseAdapter) {
            ((BaseAdapter)adapter).notifyDataSetChanged();
        }
    

提交回复
热议问题