.1458 2022-09-23 11:59 采纳率: 50%
浏览 38
已结题

android listview控件在ui上显示蓝牙设备

问题遇到的现象和发生背景

我目前在做android项目 :搜索蓝牙设备并通过列表显示 我用的是listview 使用的广播接收然后用listview的adapter 去添加设备信息 但是显示不出来

用代码块功能插入代码,请勿粘贴截图

private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
@SuppressLint("MissingPermission")
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG,"有回调");
switch (action) {
case BluetoothDevice.ACTION_FOUND:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//已经匹配的设备
String str = device.getName() + "\n" + device.getAddress();
int deviceTypeImg = BtUtil.getDeviceType(device.getBluetoothClass( ) );
Log.d(TAG, "列表:" + str + " 图片:" + Long.toHexString(deviceTypeImg) );
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
Log.d(TAG, "有匹配到设备");
bluetoothlist.add(new SPPView(deviceTypeImg, (str + "(已配对)") ) );
} else if (!bluetoothlist.contains(str)) {
Log.d(TAG, "有未匹配的设备");
bluetoothlist.add(new SPPView(deviceTypeImg, (str + "(未配对)") ) );
}
break;
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
Log.d(TAG, "开始搜索的设备");
Toast.makeText(ScanSppFragment.this, "开始搜索", Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
Log.d(TAG, "搜索完毕");
Toast.makeText(ScanSppFragment.this, "搜索完毕", Toast.LENGTH_SHORT).show();
break;
}
mAdapter = new SPPAdapter(context,bluetoothlist);
listView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
};

运行结果及报错内容

没有报错

我想要达到的结果

显示出来

  • 写回答

3条回答 默认 最新

  • love. 2022-09-23 14:23
    关注

    不是这样玩的,activity启动就先把适配器new好。然后在你的搜索蓝牙成功后去把list传给适配器,然后刷新列表即可。(确认你的数据正常,然后数据是否传给适配器并设置到相应的列表中去了,适配器中有个getcount方法,这个方法里面需要给int值,这个就是你列表数据的长度。)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月1日
  • 已采纳回答 9月23日
  • 创建了问题 9月23日

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog