首先对不起我的英语,这很糟糕。现在我正在编写一个应用程序并且列表有问题。搜索设备后,我的列表中总是出现重复项,当我再次搜索时,我再次获得相同的设备。我必须尝试使用集合,但我仍然得到重复。我不知道如何解决这个问题。请帮助某人使用代码。谢谢你提前。
这是我的代码:
`
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);
}
}
}
}
};
} `