Dynamically create CheckBoxPreferences

后端 未结 4 2208
孤城傲影
孤城傲影 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-13 23:52

    Use PreferenceFragmentCompat from Preference Compat Library

    compile 'com.android.support:preference-v7:23.4.0'

    Check this article for the implementation details https://medium.com/@arasthel92/dynamically-creating-preferences-on-android-ecc56e4f0789#.71ssvjses

    0 讨论(0)
  • 2021-02-14 00:06

    You need a ListView for that, a PreferenceActivity. As discussed in this link, PreferenceActivity should only be used for actually saving preferences.

    Instead you could either create a simple dialog with single or multiple choice options: http://developer.android.com/guide/topics/ui/dialogs.html

    Or use a ListView as in the API examples Google provides, they give a simple example:

    http://hi-android.info/docs/resources/samples/ApiDemos/src/com/example/android/apis/view/List10.html

    0 讨论(0)
  • 2021-02-14 00:07

    I think you're looking for something like this:

    public class MyPreferenceActivity extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.my_preference_activity);
    
            //fetch the item where you wish to insert the CheckBoxPreference, in this case a PreferenceCategory with key "targetCategory"
            PreferenceCategory targetCategory = (PreferenceCategory)findPreference("targetCategory");
    
            //create one check box for each setting you need
            CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);
            //make sure each key is unique  
            checkBoxPreference.setKey("keyName");
            checkBoxPreference.setChecked(true);
    
            targetCategory.addPreference(checkBoxPreference);
        }
    }
    
    0 讨论(0)
  • 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<length; i++){
            CharSequence val = ""+i+1+"";
            values[i] =  val;
        }
        list1.setEntryValues(values);
    
        cat1.addPreference(list1);
    
        return root;
    }//end method
    

    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...

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