Dynamically create CheckBoxPreferences

落爺英雄遲暮 提交于 2019-12-04 18:40:33

问题


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 CheckBoxPreference dynamically?


回答1:


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



回答2:


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




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/6272551/dynamically-create-checkboxpreferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!