在我的Android应用程序中,我有一个ListActivity,它显示蓝牙设备。我有一个ArrayList<BluetoothDevice>ArrayAdapter<BluetoothDevice>。一切正常,但是有一个问题。每个BluetoothDevice在列表中显示为MAC地址,但我需要显示其名称。

据我所知,适配器在每个对象上调用toString方法。但是,如果您在其上调用BluetoothDevice,则toString将返回MAC地址。因此解决方案将是覆盖toString并返回名称而不是地址。但是BluetoothDevice是最后一堂课,所以我不能覆盖它!

有什么想法如何强制蓝牙设备返回其名称而不是地址? toString

最佳答案

正如我在评论中已经提到的,您可以扩展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;
 }
}

09-30 17:44
查看更多