Dynamically create CheckBoxPreferences

后端 未结 4 2196
孤城傲影
孤城傲影 2021-02-13 23:18

I am currently building out a list of rows with checkboxes dynamically using content from a web service. However, this ListView will need to do pretty much what a

4条回答
  •  一整个雨季
    2021-02-14 00:07

    Well @Jodes, actually both of you are right, but the correct way of doing this would be using a ListPreference.

    I would use a entire programmatic approach, from my experience it's easier to be consistent; either create an entire XML layout via code, or via XML, but mixing the 2 can be weird and you cannot alter everything set via XML...

    onCreate(){
        this.setPreferenceScreen(createPreferenceHierarchy());
    }
    
    public PreferenceScreen createPreferenceHierarchy(){
        PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
    
        // category 1 created programmatically
        PreferenceCategory cat1 = new PreferenceCategory(this);
        cat1.setTitle("title");
        root.addPreference(cat1);
    
        ListPreference list1 = new ListPreference(this);
        list1.setTitle(getResources().getString(R.string.some_string_title));
        list1.setSummary(getResources().getString(R.string.some_string_text));      
        list1.setDialogTitle(getResources().getString(R.string.some_string_pick_title));
        list1.setKey("your_key");
    
        CharSequence[] entries  = calendars.getCalenders(); //or anything else that returns the right data
        list1.setEntries(entries);
        int length              = entries.length;
        CharSequence[] values   = new CharSequence[length];
        for (int i=0; i

    However, using this approach you will run into the platform's limitations of not having a multiple select ListPreference, and you'll probably want to implement something else.

    I found this solution, which works great. You'll have to read the comments to find clues about how to debug the code though...

提交回复
热议问题