是否可以从适配器加载 ListPreference 项目?

发布于 2024-09-03 15:00:09 字数 508 浏览 8 评论 0原文

我正在着手为我的应用程序创建一个设置活动。我定义了一个具有良好布局的 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 技术交流群。

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

发布评论

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

评论(4

2024-09-10 15:00:09

对于蓝牙设备列表的特殊情况,您可以使用以下类:

package de.duenndns;

import android.bluetooth.*;
import android.content.Context;
import android.preference.ListPreference;
import android.util.AttributeSet;

import java.util.Set;

public class BluetoothDevicePreference extends ListPreference {

    public BluetoothDevicePreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> pairedDevices = bta.getBondedDevices();
        CharSequence[] entries = new CharSequence[pairedDevices.size()];
        CharSequence[] entryValues = new CharSequence[pairedDevices.size()];
        int i = 0;
        for (BluetoothDevice dev : pairedDevices) {
            entries[i] = dev.getName();
            if (entries[i] == null) entries[i] = "unknown";
            entryValues[i] = dev.getAddress();
            i++;
        }
        setEntries(entries);
        setEntryValues(entryValues);
    }

    public BluetoothDevicePreference(Context context) {
        this(context, null);
    }

}

它可以直接从您的首选项 XML 中涉及,以将 MAC 存储为首选项字符串:

<de.duenndns.BluetoothDevicePreference
    android:key="bluetooth_mac"
    android:title="Bluetooth Device"
    android:dialogTitle="Choose Bluetooth Device" />

For the special case of a list of bluetooth devices, you can use the following class:

package de.duenndns;

import android.bluetooth.*;
import android.content.Context;
import android.preference.ListPreference;
import android.util.AttributeSet;

import java.util.Set;

public class BluetoothDevicePreference extends ListPreference {

    public BluetoothDevicePreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> pairedDevices = bta.getBondedDevices();
        CharSequence[] entries = new CharSequence[pairedDevices.size()];
        CharSequence[] entryValues = new CharSequence[pairedDevices.size()];
        int i = 0;
        for (BluetoothDevice dev : pairedDevices) {
            entries[i] = dev.getName();
            if (entries[i] == null) entries[i] = "unknown";
            entryValues[i] = dev.getAddress();
            i++;
        }
        setEntries(entries);
        setEntryValues(entryValues);
    }

    public BluetoothDevicePreference(Context context) {
        this(context, null);
    }

}

It can be involved directly from your prefs XML to store the MAC as a prefs string:

<de.duenndns.BluetoothDevicePreference
    android:key="bluetooth_mac"
    android:title="Bluetooth Device"
    android:dialogTitle="Choose Bluetooth Device" />
誰認得朕 2024-09-10 15:00:09

ListPreference 不适用于适配器,它适用于字符串。请参阅 setEntries()< /code>setEntryValues()

要获取您的 ListPreference,请调用 findPreference() 在您的 PreferenceActivity 上。将您返回的 Preference 投射回 ListPreference

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

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

ぽ尐不点ル 2024-09-10 15:00:09

只是对此进行更新,以防其他人出现,ge0rg 答案有效,但对其进行了一点更改,以考虑多种偏好,而不仅仅是蓝牙偏好,并且如果他们没有设置任何配对设备,这样您就不会收到错误带有一个空数组。

ListPreference BTList = (ListPreference) findPreference("your preference key");
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    CharSequence[] entries = new CharSequence[1];
    CharSequence[] entryValues = new CharSequence[1];
    entries[0] = "No Devices";
    entryValues[0] = "";
    if(pairedDevices.size() > 0){
        entries = new CharSequence[pairedDevices.size()];
        entryValues = new CharSequence[pairedDevices.size()];
        int i=0;
        for(BluetoothDevice device : pairedDevices){
            entries[i] = device.getName();
            entryValues[i] = device.getAddress();
            i++;
        }
    }
    BTList.setEntries(entries);
    BTList.setEntryValues(entryValues);

`希望这对某人有帮助......哦,这是放在首选项活动的 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.

ListPreference BTList = (ListPreference) findPreference("your preference key");
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    CharSequence[] entries = new CharSequence[1];
    CharSequence[] entryValues = new CharSequence[1];
    entries[0] = "No Devices";
    entryValues[0] = "";
    if(pairedDevices.size() > 0){
        entries = new CharSequence[pairedDevices.size()];
        entryValues = new CharSequence[pairedDevices.size()];
        int i=0;
        for(BluetoothDevice device : pairedDevices){
            entries[i] = device.getName();
            entryValues[i] = device.getAddress();
            i++;
        }
    }
    BTList.setEntries(entries);
    BTList.setEntryValues(entryValues);

`Hope this helps someone...oh and this is put under the onCreate method for the preference activity

2024-09-10 15:00:09

另一种方法是重写 onPrepareDialogBu​​ilder 的 ListPreference 并初始化 setSingleChoiceItems of AlertDialog.Builder 直接与您的适配器一起使用:

public class AdapterListPreference extends ListPreference
{
    @Override
    protected void onPrepareDialogBuilder( AlertDialog.Builder builder )
    {
        // don't call super.onPrepareDialogBuilder() because it'll check
        // for Entries and set up a setSingleChoiceItems() for them that
        // will never be used

        final ListAdapter adapter = …;

        builder.setSingleChoiceItems(
            adapter,
            0,
            new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick( DialogInterface dialog, int which )
                {
                    // adapter.getItemId( which )

                    dialog.dismiss();
                }
            } );

        builder.setPositiveButton( null, null );
    }
}

如果您查看 Android 源代码,您会发现 onPrepareDialogBu​​ilder() 确实会调用:

public AlertDialog.Builder setSingleChoiceItems (CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener)

使用这些条目数组。要使ListPreference 使用某些适配器(例如ArrayAdaper、CursorAdapter),您只需调用:

public AlertDialog.Builder setSingleChoiceItems (ListAdapter adapter, int checkedItem, DialogInterface.OnClickListener listener)

即可。

这样,ListPreference 将直接在适配器上操作,您不需要从适配器复制数据以将它们放入条目数组中。

此处查找工作示例。

Another way would be to override onPrepareDialogBuilder of ListPreference and to initialize setSingleChoiceItems of AlertDialog.Builder directly with your adapter:

public class AdapterListPreference extends ListPreference
{
    @Override
    protected void onPrepareDialogBuilder( AlertDialog.Builder builder )
    {
        // don't call super.onPrepareDialogBuilder() because it'll check
        // for Entries and set up a setSingleChoiceItems() for them that
        // will never be used

        final ListAdapter adapter = …;

        builder.setSingleChoiceItems(
            adapter,
            0,
            new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick( DialogInterface dialog, int which )
                {
                    // adapter.getItemId( which )

                    dialog.dismiss();
                }
            } );

        builder.setPositiveButton( null, null );
    }
}

If you look at the Android sources, you'll find that onPrepareDialogBuilder() does call:

public AlertDialog.Builder setSingleChoiceItems (CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener)

with those entry arrays. To make ListPreference use some adapter (e.g. ArrayAdaper, CursorAdapter), you just need to call:

public AlertDialog.Builder setSingleChoiceItems (ListAdapter adapter, int checkedItem, DialogInterface.OnClickListener listener)

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.

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