是否可以从适配器加载 ListPreference 项目?
我正在着手为我的应用程序创建一个设置活动。我定义了一个具有良好布局的 PreferenceActivity
,其中包括一个 ListPreference
对象,供用户选择蓝牙设备。 我在动态填充列表时遇到问题。
我想使用数组适配器中的值填充ListPreference
(我将创建该适配器并使用相关的蓝牙设备名称进行填充)。
如果这是一个旋转视图,我只需调用 setAdapter()
即可。但是,使用 ListPreference
对象,我无法弄清楚如何附加适配器(findviewByID
不会从 View 转换为 ListPreference
,所以我甚至无法获取该对象的句柄)。
我想附加一个适配器,然后用值填充适配器,这反过来又用值填充 ListPreference
。
I'm setting out to create a settings activity for my app. I've defined a PreferenceActivity
with a nice layout including a ListPreference
object for the user to select a bluetooth device. I'm having trouble dynamically populating the list.
I would like to populate ListPreference
with values from an array adapter (which I'll create and populate with relevant bluetooth device names).
If this were a spinner View, I could just call setAdapter()
. However with the ListPreference
object I can't figure out how to attach an adapter (findviewByID
won't cast from View To ListPreference
, so I can't even get a handle to the object).
I would like to attach an adapter and then populate the adapter with values, which in turn would populate the ListPreference
with values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于蓝牙设备列表的特殊情况,您可以使用以下类:
它可以直接从您的首选项 XML 中涉及,以将 MAC 存储为首选项字符串:
For the special case of a list of bluetooth devices, you can use the following class:
It can be involved directly from your prefs XML to store the MAC as a prefs string:
ListPreference
不适用于适配器,它适用于字符串。请参阅setEntries()< /code>
和
setEntryValues()
要获取您的
ListPreference
,请调用findPreference()
在您的PreferenceActivity
上。将您返回的Preference
投射回ListPreference
。ListPreference
doesn't work with adapters, it works with strings. SeesetEntries()
andsetEntryValues()
To get at your
ListPreference
, callfindPreference()
on yourPreferenceActivity
. Cast thePreference
you get back toListPreference
.只是对此进行更新,以防其他人出现,ge0rg 答案有效,但对其进行了一点更改,以考虑多种偏好,而不仅仅是蓝牙偏好,并且如果他们没有设置任何配对设备,这样您就不会收到错误带有一个空数组。
`希望这对某人有帮助......哦,这是放在首选项活动的 onCreate 方法下的
Just an Update to this in case someone else comes along, ge0rg answer works but changed it a little to take in consideration of multiple preferences and not just the bluetooth one and also if they dont have any paired devices set up so you dont get an error with a null array.
`Hope this helps someone...oh and this is put under the onCreate method for the preference activity
另一种方法是重写 onPrepareDialogBuilder 的 ListPreference 并初始化 setSingleChoiceItems of AlertDialog.Builder 直接与您的适配器一起使用:
如果您查看 Android 源代码,您会发现 onPrepareDialogBuilder() 确实会调用:
使用这些条目数组。要使ListPreference 使用某些适配器(例如ArrayAdaper、CursorAdapter),您只需调用:
即可。
这样,ListPreference 将直接在适配器上操作,您不需要从适配器复制数据以将它们放入条目数组中。
此处查找工作示例。
Another way would be to override onPrepareDialogBuilder of ListPreference and to initialize setSingleChoiceItems of AlertDialog.Builder directly with your adapter:
If you look at the Android sources, you'll find that onPrepareDialogBuilder() does call:
with those entry arrays. To make ListPreference use some adapter (e.g. ArrayAdaper, CursorAdapter), you just need to call:
instead.
This way ListPreference will operate on the adapter directly and you don't need to copy the data from the adapter to put them into the entries arrays.
Find a working sample here.