Change android preferences diplayed preferences in the same PreferenceActivity

I have two XML preference files. The first (pref2.xml) contains 2 settings:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

   <CheckBoxPreference 
        android:key="key"
        android:title="title" 
        android:defaultValue="false"
    />
   <CheckBoxPreference 
        android:key="key2"
        android:title="title" 
        android:defaultValue="false"
    />
 </PreferenceScreen>

and the other (pref1.xml) contains 1 preference:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

   <CheckBoxPreference 
        android:key="key"
        android:title="title" 
        android:defaultValue="false"
    />

 </PreferenceScreen>

in my activity preferences I try to change them:

    public void onCreate(Bundle savedInstanceState) {       
            super.onCreate(savedInstanceState);   

            addPreferencesFromResource(R.xml.pref1);
            this.getPreferenceScreen().removeAll();
            addPreferencesFromResource(R.xml.pref2);
            final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
            Log.d("LOG", "size of sharedPreferences"+adapter.getCount()+"(should be 2)");

in fact, here I get the correct output, and everything is displayed correctly. But I want to change the displayed preferences with respect to one preference. Therefore, I implemented OnSharedPreferenceChangeListener in activity preferences:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    super.onSharedPreferenceChanged(sharedPreferences, key);

    //switch the widget type
    if (key.equals("key")){
        this.getPreferenceScreen().removeAll();
                    addPreferencesFromResource(R.xml.pref1);
                    final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
                    Log.d("LOG", "size of sharedPreferences "+adapter.getCount()+" (should be 1)");
    }
 }

Well, pref1 preferences display correctly, but the output

"size sharedPreferences 2"

indicated that old preferences are still applied in the background. If I iterate over the listAdapter, I also get the old settings.

Any idea how I could solve this?

, listadapter - . getPreferenceScreen(). GetPreferenceCount() . ?

+3
2

, , "", . , , . , . .

0

: , : S

...

Okey, . PreferenceScreen listView:

 getPreferenceScreen().bind((ListView)findViewById(android.R.id.list));
0

All Articles