动态创建CheckBoxPreferences

发布于 2024-11-14 09:02:53 字数 269 浏览 3 评论 0原文

我目前正在使用网络服务中的内容动态构建带有复选框的行列表。但是,此 ListView 几乎需要完成 PreferenceActivity 所能完成的工作。

我不知道行数,因为内容是动态的,因此我无法在 XML 中创建每个 CheckBoxPreference。如何构建一个 PreferenceActivity 来动态显示带有 CheckBoxPreference 的未知行数?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桜花祭 2024-11-21 09:02:53

我想你正在寻找这样的东西:

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

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);
    }
}
不顾 2024-11-21 09:02:53

好吧@Jodes,实际上你们俩都是对的,但是正确的方法是使用 列表首选项

我会使用完整的程序化方法,根据我的经验,它更容易保持一致;要么通过代码创建整个 XML 布局,要么通过 XML,但是混合两者可能会很奇怪,并且您无法更改通过 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

但是,使用这种方法您将遇到平台的限制,即没有多重选择 < code>ListPreference,您可能想要实现其他东西。

我找到了这个解决方案,效果很好。不过,您必须阅读注释才能找到有关如何调试代码的线索......

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

童话 2024-11-21 09:02:53

为此,您需要一个 ListView,一个 PreferenceActivity。正如此链接中所述,< code>PreferenceActivity 只能用于实际保存首选项。

相反,您可以创建一个带有单选或多选选项的简单对话框:
http://developer.android.com/guide/topics/ui/dialogs.html

或者使用 ListView,如 Google 提供的 API 示例中所示,他们给出了一个简单的示例:

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

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

等你爱我 2024-11-21 09:02:53

使用 Preference Compat Library 中的 PreferenceFragmentCompat

编译 'com.android.support:preference-v7:23.4.0'

查看本文以了解实现细节 https://medium.com/@arasthel92/dynamically-creating-preferences-on-android-ecc56e4f0789#.71ssvjses

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文