Dynamic ListPreference in android

后端 未结 3 1901
自闭症患者
自闭症患者 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:07

    Every XML element in Android can be created programmatically as the element name is also a Java class. Hence you can create a ListPreference in code:

    CharSequence[] entries = { "One", "Two", "Three" };
    CharSequence[] entryValues = { "1", "2", "3" };
    ListPreference lp = new ListPreference(this);
    lp.setEntries(entries);
    lp.setEntryValues(entryValues);
    

    You could alternatively create it in XML then add the entries/entry values in code:

    CharSequence[] entries = { "One", "Two", "Three" };
    CharSequence[] entryValues = { "1", "2", "3" };
    ListPreference lp = (ListPreference)findPreference("list_key_as_defined_in_xml");
    lp.setEntries(entries);
    lp.setEntryValues(entryValues);
    

提交回复
热议问题