Add element to an Arraylist only if it does not exist (Android)

Asked

Viewed 56 times

2

I am programming a bluetooth app in Android Studio. I have an arrayList and I place devices that have been discovered in a bluetooth connection. It turns out that when I perform the task of searching new devices for the second, third ... time it add the same device being that it was already in the list.

That’s the part of the code in question:

        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                 int i = 0;
                while(i < itens.size())
                {
                    Log.i("Script", "Elemento na lista: "+itens.get(i).getNome());
                    i++;
                }
                // Get the "RSSI" to get the signal strength as integer,
                // but should be displayed in "dBm" units
                Item dispositivo = new Item();
                dispositivo.setNome(device.getName());
                dispositivo.setMac(device.getAddress());
                if (!itens.contains(device.getName())) {
                    Log.i("Script ", "Dispositivo " +device.getName()+" não existe na lista, ele foi adicionado");
                    updateList(dispositivo);
                }
            }

            }
        };

At the first execution of the method, then appears in my Log:

inserir a descrição da imagem aqui

Note that, he verified that a device did not exist in the list and added it, then to the second device, as the list already had an item he was shown (Element in the list: Galaxy J2 Prime), checked if it existed, as not, was added to the list(Jir device does not exist in the list, it has been added). So far so good!

It turns out that by method calling a second, third time, it always adds the same elements in the list:

inserir a descrição da imagem aqui

You can see that now the elements repeated in the List, I believe that the error is in the comparison, items.contais(), but I do not know how to solve, some help?

  • What kind of list itens what do you use? It’s from String? The mistake really is in itens.contains() which seems to be comparing two objects of different types one String and another that seems to be one Item.

2 answers

2

If you’re readjusting it, it means that your logic doesn’t just look for new devices, it’s a search for everyone who shows up. You can take advantage of the code by simply clearing the list before the algorithm starts.

1

This is happening because internally the contains() method of the Arraylist class makes use of the equals() method, so you will need to implement the equals method,.

@Override
public int hashCode() {
    return Objects.hash(this.name,this.adress);
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof Item)) {
        return false;
    }
    Item other = (Item) obj;
    return Objects.equals(name, other.name) && Objects.equals(adress, other.adress);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.