Android Dynamic Array ListPreference

前端 未结 1 875
天涯浪人
天涯浪人 2021-02-08 03:24

How to Create Dynamic Array For ListPreference From Java Side.

I Don\'t use Below Xml .




        
相关标签:
1条回答
  • 2021-02-08 04:06

    Place preferences.xml in res/xml

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
        <PreferenceCategory android:title="Some title">     
    
            <ListPreference android:key="default_category"
                android:title="Dynamic categories" android:summary="Dynamic categories summary"
                android:defaultValue="0" />
        </PreferenceCategory>
    </PreferenceScreen>
    

    In your activity that extends PreferenceActivity you do something like this in onCreate().

    ListPreference listPreferenceCategory = (ListPreference) findPreference("default_category");
    if (listPreferenceCategory != null) {
        ArrayList<Category> categoryList = getCategories();
        CharSequence entries[] = new String[categoryList.size()];
        CharSequence entryValues[] = new String[categoryList.size()];
        int i = 0;
        for (Category category : categoryList) {
            entries[i] = category.getCategoryName();
            entryValues[i] = Integer.toString(i);
            i++;
        }
        listPreferenceCategory.setEntries(entries);
        listPreferenceCategory.setEntryValues(entryValues);
    }
    
    0 讨论(0)
提交回复
热议问题