Android listView重复信息
Android listView duplicate information
我创建了一个蓝牙应用程序这是我的设备代码:
Set<BluetoothDevice> paireddevices = bluetoothAdapter.getBondedDevices();
if(paireddevices.size() > 0)
{
for(BluetoothDevice bluetoothDevice : paireddevices)
{
map = new HashMap<String, String>();
map.put("titre", bluetoothAdapter.getName() + " - Appairé");
map.put("description", bluetoothAdapter.getAddress());
listItem.add(map);
}
}
这是我的广播代码:
public void onClick(View v) {
//chercher des nouveaux devices
bluetoothAdapter.startDiscovery();
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
map = new HashMap<String, String>();
map.put("titre", device.getName());
map.put("description", device.getAddress());
listItem.add(map);
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(broadcastReceiver, filter);
mSchedule = new SimpleAdapter(this, listItem, R.layout.affichageitem, new String[] {"titre", "description"}, new int[] {R.id.titre, R.id.description});
listview.setAdapter(mSchedule);
}
一切正常,但是当我点击获取设备时,我得到了多个信息,如果我仍然点击越来越多,我会得到更多重复信息:(
http://www.hostingpics.net/viewer.php?id=48265220150914173852.jpg
我怎样才能只得到一台 LG G3D855 和一台三星 galaxy S5,以及一台 HC-05...
谢谢
我建议使用此功能向您的 listItem 添加任何内容:
//WARNING I did not try this code, handle it as pseudo-code:
private void addIfNotContains(HashMap<String, String> map){
boolean contains = false;
for(HashMap<String, String> m : listItem){
if(m.get("description").equals(map.get("description"))) contains = true;
}
if(!contains) listItem.add(map);
}
并使用每个地方而不是 listItem.add(map)
addIfNotContains(map)
。我认为这会起作用。
我在服务方面遇到了同样的情况,我捕获了关键事件并使用了 arrayList.clear(); ,因此它将刷新以前的数据并覆盖新数据
希望这会对你有所帮助
我创建了一个蓝牙应用程序这是我的设备代码:
Set<BluetoothDevice> paireddevices = bluetoothAdapter.getBondedDevices();
if(paireddevices.size() > 0)
{
for(BluetoothDevice bluetoothDevice : paireddevices)
{
map = new HashMap<String, String>();
map.put("titre", bluetoothAdapter.getName() + " - Appairé");
map.put("description", bluetoothAdapter.getAddress());
listItem.add(map);
}
}
这是我的广播代码:
public void onClick(View v) {
//chercher des nouveaux devices
bluetoothAdapter.startDiscovery();
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
map = new HashMap<String, String>();
map.put("titre", device.getName());
map.put("description", device.getAddress());
listItem.add(map);
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(broadcastReceiver, filter);
mSchedule = new SimpleAdapter(this, listItem, R.layout.affichageitem, new String[] {"titre", "description"}, new int[] {R.id.titre, R.id.description});
listview.setAdapter(mSchedule);
}
一切正常,但是当我点击获取设备时,我得到了多个信息,如果我仍然点击越来越多,我会得到更多重复信息:(
http://www.hostingpics.net/viewer.php?id=48265220150914173852.jpg
我怎样才能只得到一台 LG G3D855 和一台三星 galaxy S5,以及一台 HC-05...
谢谢
我建议使用此功能向您的 listItem 添加任何内容:
//WARNING I did not try this code, handle it as pseudo-code:
private void addIfNotContains(HashMap<String, String> map){
boolean contains = false;
for(HashMap<String, String> m : listItem){
if(m.get("description").equals(map.get("description"))) contains = true;
}
if(!contains) listItem.add(map);
}
并使用每个地方而不是 listItem.add(map)
addIfNotContains(map)
。我认为这会起作用。
我在服务方面遇到了同样的情况,我捕获了关键事件并使用了 arrayList.clear(); ,因此它将刷新以前的数据并覆盖新数据 希望这会对你有所帮助