0

I have a ArrayList of Metricas. Each Metrica has some properties and another ArrayList lForms. What I want to to is check in the db to see if the form has been filled, and update the lForms. When I do so, I get a ConcurrentModificationException, I don't know how to get rid of it...

public ArrayList<Metrica> addAllForms(ArrayList<Metrica> lMetricas) {
    ArrayList<Metrica> newlMet = lMetricas;
    ArrayList<Formulario> lForms = null;
    ArrayList<Formulario> newlForms;
    Metrica newMetrica;
    Formulario f;
    for (Iterator<Metrica> it = lMetricas.iterator(); it.hasNext();) {
        Metrica metrica = it.next();

        newlForms = new ArrayList<Formulario>();
        lForms = metrica.getlForms();
        for (Iterator<Formulario> it2 = lForms.iterator(); it2.hasNext();) {
            Formulario form = it2.next();

            f = getForm(form);
            if (f == null) {
                addForm(form, metrica.getId());
                Log.e("log", "adding form");
            } else
                form = f;
            Log.e("log", "Getting adding form");

            newlForms.add(form);

        }

        newMetrica = new Metrica();
        newMetrica.setlForms(newlForms);
        newMetrica.setClienteId(metrica.getClienteId());
        newMetrica.setDatoValido(metrica.getDatoValido());
        newMetrica.setDescription(metrica.getDescription());
        newMetrica.setId(metrica.getId());
        newMetrica.setProyectoProductoId(metrica.getProyectoProductoId());
        newMetrica.setTipo(metrica.getTipo());

        newlMet.add(newMetrica);

    }

    return lMetricas;
}
Juliatzin
  • 18,455
  • 40
  • 166
  • 325

1 Answers1

0

Instead of

for (Metrica metrica : lMetricas) 

Use the following:

Iterator<Metrica> it = lMetricas.iterator();
while(it.hasNext())
{
    Metrica metrica = it.next();
    //...
}

Similarly, you need to do the same ting with the lForms.

Edit: If you want to add something, you cannot just add them while iterating. Instead, use another ArrayList to store the elements you want to add. Afterwards, use lMetricas.addAll() to add them all at once.

If you want to add them to specific positions, you need to use a Map<Metrica, Integer> and use the values to find which position to add.

padawan
  • 1,295
  • 1
  • 17
  • 45