是否有可能从一个适配器加载列表preference项目?有可能、适配器、加载、项目

2023-09-06 04:16:58 作者:星星夹在书页中

我设置了​​创建为我的应用程序设置一个活动。我已经定义了一个 preferenceActivity 有一个很好的布局,包括目录preference 目标用户选择一个蓝牙设备。 我无法动态填充列表

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.

我想填充目录preference 从一个阵列适配器值(我将创建并填充与相关蓝牙设备名称)。

I would like to populate ListPreference with values from an array adapter (which I'll create and populate with relevant bluetooth device names).

如果这是一个微调视图,我可以叫 setAdapter()。然而,随着目录preference 对象我无法弄清楚如何将一个适配器( findviewByID 荣获'牛逼投从视野中目录preference ,所以我甚至不能得到一个处理的对象)。

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

我要附加一个适配器,然后填充适配器值,这反过来会填充目录preference 与价值观。

I would like to attach an adapter and then populate the adapter with values, which in turn would populate the ListPreference with values.

推荐答案

目录preference 不带适配器的工作,这也适用于字符串。请参阅setEntries()和setEntryValues()

ListPreference doesn't work with adapters, it works with strings. See setEntries() and setEntryValues()

要得到你的目录preference ,叫find$p$pference()在 preferenceActivity 。铸 preference 你回到目录preference

To get at your ListPreference, call findPreference() on your PreferenceActivity. Cast the Preference you get back to ListPreference.