Programmaticaly创建动态复选框preferences Android中 [英] Programmaticaly Create Dynamic CheckBoxPreferences in Android
问题描述
我目前正在构建具有动态的复选框使用内容从Web服务的一组行。然而,这ListView的将需要做的pretty的多少什么preferenceActivity将完成。我不知道的行数的内容是动态的,所以我不能创建每个复选框preference XML格式。我如何着手建立一个preferenceActivity,将显示一个未知数量的行用一个CheckBox prefence动态使用严格的Java?
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 PreferenceActivity would accomplish. I don't know the number of rows as the content is dynamic so I can't create each CheckBoxPreference in xml. How do I go about building a PreferenceActivity that will display an unknown number rows with a CheckBoxPrefence dynamically using strictly Java?
由于时间提前帮助。
推荐答案
我认为你在寻找这样的事情:
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);
}
}
这篇关于Programmaticaly创建动态复选框preferences Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!