Dynamic ListPreference in android

后端 未结 3 1905
自闭症患者
自闭症患者 2020-12-02 10:30

How to generate dynamic listPreference in android? I want to get all wifi access points and make a list using in preference Activity(i.e. make a list using listpreference).

3条回答
  •  甜味超标
    2020-12-02 11:18

    This minimalist technique is for both environments.

    In preferences.xml

    
    
    

    In PreferenceFragment.onCreate()

    addPreferencesFromResource(R.xml.preferences);
    expand_xyzzy((ListPreference)findPreference("xyzzy"));
    

    Elsewhere

    public static Preference expand_xyzzy (ListPreference pref) {
        if (pref == null) return pref;
        pref.setEntries(new String["one","two","three];
        pref.setEntryValues(new String["0","1","2"]);
        return pref;
    }
    

    Notes:
    (a) XML is self-documenting and perhaps a better choice than dynamic preference creation.
    (b) Starting your PreferenceFragment by NOT using PreferenceActivity easily lets you do this:

提交回复
热议问题