Android listView重复信息

Android listView重复信息,android,android-listview,bluetooth,Android,Android Listview,Bluetooth,我创建了一个蓝牙应用程序这是我的设备代码: Set<BluetoothDevice> paireddevices = bluetoothAdapter.getBondedDevices(); if(paireddevices.size() > 0) { for(BluetoothDevice bluetoothDevice : paireddevices) { map = new HashMap<String, String>()

我创建了一个蓝牙应用程序这是我的设备代码:

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);
    }
}
Set paireddevices=bluetoothAdapter.getBondedDevices();
如果(paireddevices.size()>0)
{
用于(蓝牙设备蓝牙设备:paireddevices)
{
map=新的HashMap();
map.put(“titre”,bluetoothAdapter.getName()+“-Appairé”);
put(“description”,bluetoothAdapter.getAddress());
添加(地图);
}
}
这是我的广播代码:

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);

    }
public void onClick(视图v){
//chercher des nouveaux设备
bluetoothAdapter.startDiscovery();
broadcastReceiver=新的broadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
map=新的HashMap();
map.put(“titre”,device.getName());
map.put(“description”,device.getAddress());
添加(地图);
}
}
};
IntentFilter筛选器=新的IntentFilter(BluetoothDevice.ACTION\u已找到);
寄存器接收器(广播接收器、滤波器);
msSchedule=new simpledapter(this,listItem,R.layout.affichageitem,新字符串[]{“titre”,“description”},新int[]{R.id.titre,R.id.description});
setAdapter(mSchedule);
}
所有这些都可以工作,但当我单击“获取设备”时,我会获得多个信息,如果我仍然单击越来越多,我会获得更多重复信息:(

我如何才能只得到一个LG G3D855和一个三星galaxy S5,以及一个HC-05


谢谢

我建议使用此功能向列表项添加任何内容:

//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);
}
//警告:我没有尝试此代码,请将其作为伪代码处理:
私有void addifynotcontains(HashMap映射){
布尔包含=假;
for(HashMap m:listItem){
如果(m.get(“description”).equals(map.get(“description”))包含=true;
}
如果(!包含)listItem.add(映射);
}

并使用每个位置,而不是
列表项。添加(地图)
添加不包含(地图)
。我认为这会起作用。

我建议使用此功能向列表项添加任何内容:

//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);
}
//警告:我没有尝试此代码,请将其作为伪代码处理:
私有void addifynotcontains(HashMap映射){
布尔包含=假;
for(HashMap m:listItem){
如果(m.get(“description”).equals(map.get(“description”))包含=true;
}
如果(!包含)listItem.add(映射);
}

使用每个位置,而不是
listItem.add(map)
addIfNotContains(map)
。我想这会起作用。

我在服务方面遇到了同样的情况,我捕获了关键事件并使用了arrayList.clear();,因此它将刷新以前的数据并覆盖新数据
希望这将对您有所帮助

我在服务方面也遇到了同样的问题我捕获了关键事件并使用了arrayList.clear();,因此它将刷新以前的数据并覆盖新数据
希望这将对您有所帮助

谢谢,但是我在for(m:listItem)for(m:listItem){if(m.get(“description”).equals(map.get(“description”))contains=true;}hi;这是所有错误:我正在尝试以下操作:private void addIfNotContains(HashMap map){boolean contains=false;for(ArrayList m:listItem){if(m.get(“description”).equals(map.get(“description”)){contains=true;}}if(!contains)listItem.add(map);}但是我在m.get(“description”)get(int)中得到一个错误是的,m是HasMap而不是ArrayList,让我修改我的codePro,非常感谢你,我爱你,真的谢谢你Hanks但是我在for(m:listItem)for(m:listItem){if(m.get(“description”).equals(map.get(“description”))contains=true;}您好;这都是错误:我正在尝试以下操作:private void addIfNotContains(HashMap map){boolean contains=false;for(ArrayList m:listItem){if(m.get(“description”).equals(map.get(“description”)){contains=true;}if(!contains)listItem.add(map);}但是我在m.get(“description”)get(int)中得到一个错误是的,m是HasMap而不是ArrayList,让我修改我的codePro,非常非常感谢,我爱你,真的谢谢你