Dynamic ListPreference in android

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

    For creating a dynamic list preference, u need to create a preference activity (ie to extend an activity as PreferenceActivity).

    The following code can be used to create the list dynamically.

    // Root
            PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
            dialogBasedPrefCat.setTitle("Category Title");
            root.addPreference(dialogBasedPrefCat); //Adding a category
    
     // List preference under the category
            ListPreference listPref = new ListPreference(this);
            listPref.setKey("keyName"); //Refer to get the pref value
            listPref.setEntries("Array of values");
            listPref.setEntryValues("Array of item value");
            listPref.setDialogTitle("Dialog Title"); 
            listPref.setTitle("Title");
            listPref.setSummary("Summary");
            dialogBasedPrefCat.addPreference(listPref); Adding under the category
    
            return root;
    
    

    Hope this helps to get an !dea...

    EDIT:

    Create and add values to CharSequence[] like this:

    CharSequence[] cs = new String[]{"myValue"};
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-02 11:18

    This minimalist technique is for both environments.

    In preferences.xml

    <!-- NB: Dynamic array insertion for 'entries'/'entryValues' -->
    <ListPreference
        android:key="xyzzy"
        android:title="..."
        android:summary="..."
        android:numeric="integer"
        android:defaultValue="0"
        android:layout="?PrefLayoutDtl" 
    />
    

    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:

    0 讨论(0)
提交回复
热议问题