动态创建CheckBoxPreferences
我目前正在使用网络服务中的内容动态构建带有复选框的行列表。但是,此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想你正在寻找这样的东西:
I think you're looking for something like this:
好吧@Jodes,实际上你们俩都是对的,但是正确的方法是使用
列表首选项
。我会使用完整的程序化方法,根据我的经验,它更容易保持一致;要么通过代码创建整个 XML 布局,要么通过 XML,但是混合两者可能会很奇怪,并且您无法更改通过 XML 设置的所有内容...
但是,使用这种方法您将遇到平台的限制,即没有多重选择 < 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...
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...
为此,您需要一个
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, aPreferenceActivity
. 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
使用 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