Pick up element in Arraylist

Asked

Viewed 4,988 times

0

I’m having a problem that line:

lista p = lista.get(i);

does not want to take the element.

Look at the method below:

private void verPessoas() {
        ArrayList<RespostasAguaCasa> lista = new Read().getLista();

        for (int i = 0; i < lista.size(); i++) {
            lista p = lista.get(i);
            System.out.println("#" + i + " Nome: " + p.getNome() + ", Idade: " + p.getIdade() + " anos, Peso: " + p.getPeso() + "Kg, Possui deficiencia? " + (p.isDeficiente() ? "SIM." : "NÃO.") + " UID: " + p.getUID());        

        if (pessoas.size() == 0) System.out.println("# Não existem registros.");
    }

Read:

import model.RespostasAguaCasa;

public class Read {
 public ArrayList<RespostasAguaCasa> getLista() {
    SQLiteDatabase db = Maindb.getInstancia().getWritableDatabase();

    String query = "SELECT * FROM " + Maindb.TABELA;

    ArrayList<RespostasAguaCasa> lista = new ArrayList<>();

    Cursor c = db.rawQuery(query, null);
    if (c.moveToFirst()) {

        do {
            RespostasAguaCasa resp = new RespostasAguaCasa(c.getString(0));

            resp.setValoragua(c.getInt(1));
            resp.setAcordar(c.getInt(2));
            resp.setDormir(c.getInt(3));
            lista.add(resp);
        }
        while (c.moveToNext());
        {
        }
    }
    c.close();
    return lista;
}
}

1 answer

2

Focus on this line:

ArrayList<RespostasAguaCasa> lista = new ArrayList<>();

The list object is a ArrayList of RespostasAguaCasa

The problem is that in the next line, you confuse several things:

lista p = lista.get(i);

1º assigns say that variable P is of type list

2nd, list is a variable that has already been assigned(to such Arraylist where you do the get)

So what you want is:

RespostasAguaCasa p = lista.get(i);
  • thank you gave it right

  • @Aleanderrayson marks the answer as accepted, for the topic to be closed!

Browser other questions tagged

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