覆盖最终 BluetoothDevice 的 toString() 方法 class
Override toString() method of final BluetoothDevice class
在我的 Android 应用程序中,我有一个显示蓝牙设备的 ListActivity
。我有一个 ArrayList<BluetoothDevice>
和 ArrayAdapter<BluetoothDevice>
。一切正常,但有一个问题。每个 BluetoothDevice
在列表中显示为 MAC 地址,但我需要显示其名称。
据我所知,适配器在每个对象上调用 toString
方法。但是 BluetoothDevice
return 的 MAC 地址如果你调用 toString
就可以了。所以解决方案是覆盖 toString
和 return 名称而不是地址。但是 BluetoothDevice
是最终的 class 所以我无法覆盖它!
有什么想法可以强制蓝牙设备 return 它的名称而不是地址吗? toString
?
一旦你有了 ArrayList
ArrayList<BluetoothDevice> btDeviceArray = new ArrayList<BluetoothDevice>();
ArrayAdapter<String> mArrayAdapter;
现在您可以在 onCreateView 中添加设备,例如:
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1);
setListAdapter(mArrayAdapter);
Set<BluetoothDevice> pariedDevices = mBluetoothAdapter.getBondedDevices();
if(pariedDevices.size() > 0){
for(BluetoothDevice device : pariedDevices){
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
btDeviceArray.add(device);
}
}
所以注意可以用.getName()
方法获取名字。这解决了您的问题?
你可以使用组合而不是继承:
public static class MyBluetoothDevice {
BluetoothDevice mDevice;
public MyBluetoothDevice(BluetoothDevice device) {
mDevice = device;
}
public String toString() {
if (mDevice != null) {
return mDevice.getName();
}
// fallback name
return "";
}
}
当然你的 ArrayAdapter
会使用 MyBluetoothDevice
而不是 BluetoothDevice
正如我在评论中提到的,您可以扩展 ArrayAdapter 和
使用另一种方法代替 toString 方法。
例如:
public class YourAdapter extends ArrayAdapter<BluetoothDevice> {
ArrayList<BluetoothDevice> devices;
//other stuff
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//get view and the textView to show the name of the device
textView.setText(devices.get(position).getName());
return view;
}
}
在我的 Android 应用程序中,我有一个显示蓝牙设备的 ListActivity
。我有一个 ArrayList<BluetoothDevice>
和 ArrayAdapter<BluetoothDevice>
。一切正常,但有一个问题。每个 BluetoothDevice
在列表中显示为 MAC 地址,但我需要显示其名称。
据我所知,适配器在每个对象上调用 toString
方法。但是 BluetoothDevice
return 的 MAC 地址如果你调用 toString
就可以了。所以解决方案是覆盖 toString
和 return 名称而不是地址。但是 BluetoothDevice
是最终的 class 所以我无法覆盖它!
有什么想法可以强制蓝牙设备 return 它的名称而不是地址吗? toString
?
一旦你有了 ArrayList
ArrayList<BluetoothDevice> btDeviceArray = new ArrayList<BluetoothDevice>();
ArrayAdapter<String> mArrayAdapter;
现在您可以在 onCreateView 中添加设备,例如:
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1);
setListAdapter(mArrayAdapter);
Set<BluetoothDevice> pariedDevices = mBluetoothAdapter.getBondedDevices();
if(pariedDevices.size() > 0){
for(BluetoothDevice device : pariedDevices){
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
btDeviceArray.add(device);
}
}
所以注意可以用.getName()
方法获取名字。这解决了您的问题?
你可以使用组合而不是继承:
public static class MyBluetoothDevice {
BluetoothDevice mDevice;
public MyBluetoothDevice(BluetoothDevice device) {
mDevice = device;
}
public String toString() {
if (mDevice != null) {
return mDevice.getName();
}
// fallback name
return "";
}
}
当然你的 ArrayAdapter
会使用 MyBluetoothDevice
而不是 BluetoothDevice
正如我在评论中提到的,您可以扩展 ArrayAdapter 和 使用另一种方法代替 toString 方法。
例如:
public class YourAdapter extends ArrayAdapter<BluetoothDevice> {
ArrayList<BluetoothDevice> devices;
//other stuff
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//get view and the textView to show the name of the device
textView.setText(devices.get(position).getName());
return view;
}
}