【发布时间】:2012-10-19 08:51:28
【问题描述】:
我将设备列表添加到 ArrayAdapter 并在 ListView 中显示,其中设备列表是从蓝牙扫描中获取的,扫描的设备首先添加到 arrayadapter。然后稍后我将其添加到列表视图中,以向用户显示扫描的蓝牙设备列表。
但是当我扫描设备时,正在添加重复设备,假设扫描设备 A 意味着然后再次是其显示设备A 的两倍或三倍。我只想显示一次扫描设备的列表。如何实现它。抱歉,如果问题含糊不清。
以下代码用于查询新设备并将其添加到arrayadapter:
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED)
{
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}
它是 Android 4.1 示例中的程序,示例名称为Bluetooth Chat。其中活动为DeviceListActivityScan.java。
【问题讨论】:
-
请在此处发布您的代码,以便我们看到您在哪里做错了..
-
将 Set 与 ArrayAdapter 一起使用。或者尝试本地化重复开始的错误
-
在扫描前尝试清除 ArrayAdapter 中的数据
-
arrayadapter 需要列表,未设置。您需要使用 getPosition 方法并覆盖项目的 equals 方法
-
@zetsin:第一次没问题,一次扫描它自己添加多次。
标签: android bluetooth android-listview android-arrayadapter