我有一个列出蓝牙设备的列表视图,但每次发现蓝牙设备时,列表视图行都会被覆盖。使用fetch.clear()方法清除arraylist对象以避免同一蓝牙设备的多个条目。将fetch.clear()方法放在其他地方会创建相同蓝牙设备的多个条目。如何停止覆盖listview中的同一行?
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();
}
}
});
答案 0 :(得分:0)
使用两个列表 - 一个发送到适配器的列表,另一个列在每个发现上。完成发现后,切换它们。这样每次发现它只会更新一次。