[英]How to tell a spinner item which method should it call when loading is over?
I have this code: 我有以下代码:
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));
}
}
And in spinner list I am getting MAC address, how I can change it for name()? 在微调器列表中,我正在获取MAC地址,如何更改其name()?
Add the name to a list instead of the whole device. 将名称添加到列表,而不是整个设备。
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));
}
}
The alternative solution is to implement your own adapter class that extends ArrayAdapter<BluetoothDevice>
that will call getItem(position).getName()
另一种解决方案是实现您自己的适配器类,该类扩展了
ArrayAdapter<BluetoothDevice>
,它将调用getItem(position).getName()
Thank that was very helpful. 谢谢,这非常有帮助。 What I did is:
我所做的是:
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.