Get element in ArrayList

0

I'm having a problem with the line:

lista p = lista.get(i);

You do not want to get 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;
}
}
    
asked by anonymous 26.05.2018 / 20:50

1 answer

2

Focus on this line:

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

The object list is a ArrayList of RespostasAguaCasa

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

lista p = lista.get(i);

1st assignment says that the variable P is of type list

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

So what you want is:

RespostasAguaCasa p = lista.get(i);
    
26.05.2018 / 21:50