Replace object within an android arraylist

3

I have an arraylist that is filled with objects from a bluetooth scanner and I show it in a recycler. What I want to do is if there is one that does not add it and only replace its value.

This object brings the distance by moving it, I am receiving a new one, so I need to modify the distance value only without adding it again.

 for( int i =0; i < datos.size() ; i++ ){

     if (!datos.contains(beaconModel))
     {
          datos.add(beaconModel);
          adapter = new BeaconAdapter(datos, RangingActivity.this);
          recyclerView.setAdapter(adapter);
          recyclerView.setLayoutManager(layoutManager);
          Log.d("datos", datos.toString());
          datos.set(i,beaconModel);
      }
      else if (datos.contains(beaconModel)){
          datos.set(i,beaconModel);
          adapter = new BeaconAdapter(datos, RangingActivity.this);
          recyclerView.setAdapter(adapter);
          recyclerView.setLayoutManager(layoutManager);
          Log.d("datos", datos.toString());
      }
 }

With this I have been able to update the data but it adds it a thousand times xD.

BeaconModel is the object. datos is the arraylist that contains those objects.

    
asked by Aldo Gallardo 16.09.2016 в 17:54
source

2 answers

1

I think the problem you're having is because of if (!datos.contains(beaconModel))

I think you should overwrite the equals() of your BeaconModel method to check if two elements are the same so that you think it's convenient, since according to Javadoc for List the contains() method uses the equals() function %

For example in the following way (I have invented a bit the structure of the BeaconModel class as an example since you have not put the code of it):

public class BeaconModel 
{
    public int id; 
    public int distancia;   

    public BeaconModel (int x)
    {
        this.distancia = x;
    }

    @Override
    public boolean equals(Object object)
    {

        if (object != null && object instanceof BeaconModel )
        {
            return (this.id== ((BeaconModel ) object).id);
        }

        return false;
    }
}

This method can be auto generated together with the hashCode() method in the Android Studio doing right click inside your class, then press Generate and then equals () and hashCode () and follow the steps indicated.

    
answered by 29.12.2016 в 10:28
0

I imagine that what you add has identifying fields that differentiate them from each other, for example: Names, Id, categories, etc. It would be most convenient to find the ID or the identifier field and replace the value you want. The code that you left adds a thousand times your value, because it looks for sequence matches, I imagine that what you add has a name and when you find it adds it.

public class Dato {
     String nombre;
     String valor;
     int id;
}

//Metodo
for( int i =0; i < datos.size() ; i++ ){
    if(datos[i].id == beaconModel.id){
        datos[i].valor = beaconModel.valor;
    }
}

I imagine something like this, since you have not left all your code I assume that you could modify your model

    
answered by 17.10.2016 в 13:45