简体   繁体   中英

Change second Listpreference values in preference fragment when first Listpreference value selected

I have two Listprefence in a preference fragment and I want to refresh second Listpreference values when in first is selected something. The Listpreferences are filled over the internet.

public class array extends PreferenceFragment {
public static String apref;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);

    final ListPreference array1Preference = (ListPreference)findPreference("array1");
    setArray1PreferenceData(array1Preference);
    array1Preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    setArray1PreferenceData(array1Preference);
                    return true;}});

    final ListPreference array2Preference = (ListPreference)FindPreference("array2");
    setArray2PreferenceData(array2Preference);
    array2Preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    setArray2PreferenceData(array2Preference);
                    return true;}});
}

public void setArray1PreferenceData(ListPreference array1Preference) {
    new LoadArray1().execute();
}

public void setArray2PreferenceData(ListPreference array2Preference) {
    String CPref = "array1";
    SharedPreferences prefs = this.getActivity().getSharedPreferences(
            "com.asdd.ck_preferences", Activity.MODE_PRIVATE);
    apref = prefs.getString(CPref, "");

    if (apref != "0") {
        new LoadArray2().execute();
    } else {
        new LoadArray1().execute();
    }
}

public class LoadArray1 extends AsyncTask<String, String, String> { 
        }
public class LoadArray2 extends AsyncTask<String, String, String> {
        }

}

Finally I have got the answer my self.

array1Preference
                .setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

                    @Override
                    public boolean onPreferenceChange(Preference preference,
                            Object newValue) {

                        apref = (String) newValue;//Sets the new value for second list loader (LoadArray2)
                        PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putString(key, "0").commit();//Clear in the preference file the 2nd listpreference selection data.
                        setArray2PreferenceData(array2Preference);//Reload data from server
                        array2Preference.setValue("0");//When in 1st selected something clear in 2nd the selection.
                        return true;
                    }
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM