从 ListPreference 中删除数组项
我的偏好活动中有一个加速度计模式的列表偏好,我纯粹从 xml 加载它。如果用户设备没有或不支持加速度计,我不会完全删除阵列中的项目。我是否必须为该选项创建一个单独的列表首选项才能将其删除? (我更愿意将它们全部保留在一个中。)如何通过以下设置将其删除?
谢谢你的提示。
贾森
<string-array name="mode_text">
<item>Others Modes</item>
<item>Others Modes</item>
<item>Accelerometer Mode</item>
</string-array>
<string-array name="mode_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources>
I have a List Preference for Accelerometer mode in my preference activity which I'm loading purely from xml. If a users device does not have or support an Accelerometer I wont to completely remove the item in the array. Do I have to create a separate list preference for this one option to remove it? (I would prefer to keep them all in one.) How can it be removed with the below setup?
Thanks for the tip.
Jason
<string-array name="mode_text">
<item>Others Modes</item>
<item>Others Modes</item>
<item>Accelerometer Mode</item>
</string-array>
<string-array name="mode_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您说要“完全删除数组中的项目”时,您的意思是希望 ListPreference 不显示在 PreferenceScreen 中吗?或者您希望它在尝试获取设置值时返回空值?
好吧,我现在明白了。我认为你不能只删除数组中的那个项目。您必须以编程方式为 ListPreference 的条目和entryValues 设置新数组。
您可以通过其键找到 ListPreference。
这是一个示例:
这会将您的列表首选项更改为仅具有这两个选项。
When you say you want to "completely remove the item in the array", do you mean you want the ListPreference to not show in the PreferenceScreen? Or you want it to return a null value when you try to get the set value?
Okay, I understand now. I think you can't remove just that item in the array. You have to set new arrays for the entries and entryValues for your ListPreference, programatically.
You find the ListPreference by its key.
Here is an example:
That will change your list preference to have only those two options.