Dynamically create CheckBoxPreferences

后端 未结 4 2201
孤城傲影
孤城傲影 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

    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);
        }
    }
    

提交回复
热议问题