For does not scroll through the entire list

1

I have a problem scrolling through a list.

My for looks like this:

 for (int i = 0; i < listaCaixaAbertos().size(); i++) {
                listaCaixaAbertos().get(i).setFechado(Boolean.TRUE);
                salvar(listaCaixaAbertos().get(i));
            }

My list looks like this:

 public List<LancamentoCaixa> listaCaixaAbertos() {
    Query q = em.createQuery("FROM LancamentoCaixa As a WHERE a.fechado = false");
    return q.getResultList();
}

Every time I scroll through the list I want to set false to variable fechado .

But it does not change all the data in this list, to change all I have to click several times depending on the amount of items.

    
asked by anonymous 01.02.2018 / 18:48

1 answer

3

I think this is what you want:

for (LancamentoCaixa lancamento : listaCaixaAbertos()) {
    lancamento.setFechado(true);
    salvar(lancamento);
}

Whenever possible, the for that runs the entire collection is best. This in itself already eliminates the problems. It was ordering everything to get the size, then listing again to get the item and change it, and then list again to save. This is not only semantically wrong, it can create unwanted side effects, race condition and performance will suffer.     

01.02.2018 / 19:03