我有一个列出蓝牙设备的列表视图,但是每次发现蓝牙设备时,列表视图行都会被覆盖。我正在使用 fetch.clear() 方法清除 arraylist 对象,以避免同一蓝牙设备的多个条目。将 fetch.clear() 方法放在其他任何地方都会创建同一蓝牙设备的多个条目。如何停止覆盖列表视图中的同一行?
ArrayList<Custom> fetch = new ArrayList<Custom>();
btOnOff.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(btOnOff.isChecked()){
btAdapter.startDiscovery();
setProgressBarIndeterminateVisibility(true);
bcReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
deviceName = device.getName();
currentDateTime = DateFormat.getDateTimeInstance().format(new Date());
fetch.clear(); //clear arraylist object
Custom data = new Custom(deviceName, currentDateTime);
fetch.add(data);
lv.setAdapter(cAdapter);
}else if(btAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
btAdapter.startDiscovery();
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(bcReceiver, filter);
} else {
btAdapter.cancelDiscovery();
}
}
});