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

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日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来