首先对不起我的英语是一件坏事。 现在我正在编写一个应用程序,并且列表中存在问题。 我总是在我的名单重复搜索设备后,当我再次搜索,我再次得到相同的设备。我必须尝试使用套,但我仍然重复。 我不知道如何解决这个问题。 请用代码帮助别人。 谢谢提前。重复在ListView蓝牙
这里是我的代码:
`
public void Init(){
foundDevicesSet = new HashSet<String>();
tBtn = (ToggleButton)findViewById(R.id.toggleButton1);
availableDivce = (TextView)findViewById(R.id.textView2);
bluetoohOff =(TextView)findViewById(R.id.textView1);
DeviceList = (ListView)findViewById(R.id.listView1);
searchButton =(Button)findViewById(R.id.button1);
deviceNameAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);
DeviceList.setAdapter(deviceNameAdapter);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
receiver = 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 newDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (newDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
foundDevicesSet.add(newDevice.getName() + "|" + newDevice.getAddress());
for(String one : foundDevicesSet){
deviceNameAdapter.add(one);
}
}
}
}
};
} `
不是一个真正的通用解决方案,但+1,很好的答案 – avalancha