如何告诉一个微调器项目加载结束时应该调用哪个方法?

时间:2018-02-13 05:04:06

标签: android android-spinner

我有这段代码:

List<BluetoothDevice> devices;
if (BluetoothDevice.ACTION_FOUND.equals(action)){
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if(device.getName() != null) {
                devices.add(device);
                Log.i("FOUND!", device.getName());
                devicesSpinner.setAdapter(new ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, devices));
            }
        }

在微调列表中我得到MAC地址,我怎么能改名为name()?

2 个答案:

答案 0 :(得分:0)

将名称添加到列表而不是整个设备。

final List<String> names = new ArrayList<>();
if (BluetoothDevice.ACTION_FOUND.equals(action)){
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    final String name = device.getName();
    if(name != null) {
        names.add(name);
        Log.i("FOUND!", name);
        devicesSpinner.setAdapter(
            new ArrayAdapter<String>(context, 
               android.R.layout.simple_spinner_dropdown_item, 
               names));
    }
}

替代解决方案是实现您自己的扩展ArrayAdapter<BluetoothDevice>的适配器类,该类将调用getItem(position).getName()

答案 1 :(得分:0)

感谢非常有帮助。我做的是:

if (BluetoothDevice.ACTION_FOUND.equals(action)){
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if(device.getName() != null) {
                devices.add(device);
                Log.i("FOUND!", device.getName());
                ArrayAdapter arrayAdapter = new ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, devices){
                    @Nullable
                    @Override
                    public Object getItem(int position) {
                        return devices.get(position).getName();
                    }
                };
                devicesSpinner.setAdapter(arrayAdapter);

            }
        }