[英]How to tell a spinner item which method should it call when loading is over?
我有以下代码:
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()?
将名称添加到列表,而不是整个设备。
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()
谢谢,这非常有帮助。 我所做的是:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.