How to use Select

0

I was able to save information to my SQLite database, but I can not call them.

I'm trying using the following method:

private void verPessoas() {
    ArrayList<RespostasAguaCasa> pessoas = new Read().getLista();
    for (int i = 0; i < pessoas.size(); i++) {
        RespostasAguaCasa p = pessoas.get(i);
        int a = p.getValoragua();
        impri.setText("+"+ a);
    }

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

READ

      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.setId(c.getInt(1));
                    resp.setValoragua(c.getInt(2));
                    resp.setAcordarhora(c.getInt(3));
                    resp.setAcordarminu(c.getInt(4));
                    resp.setDormirhora(c.getInt(5));
                    resp.setDormirminu(c.getInt(6));
                    lista.add(resp);
                }
                while (c.moveToNext());
                {
                }


    }
    c.close();
    return lista;
}

}
    
asked by anonymous 10.06.2018 / 22:25

1 answer

2

1 - Do you need to start the People table with a value?

In case of (Person person = new Person (c.getString (0));) we left an empty constructor for the Table, so we can call it like this:

Pessoa pessoa = new Pessoa();
pessoa.setNome("nome");

2 - Call the results from the columns:

pessoa.setNome(c.getString(c.getColumnIndex(Coluna_Nome)));

3 - In the case of (person.setDeficiency (c.getInt (4) == 1);) it would be better something like:

Int valor_temporario = 0;
if(c.getInt(c.getColumnIndex(Tabela_Deficiencia)) == 1){
valor_temporario = 1;
}else{
valor_temporario = 0;
}
pessoa.setDeficiencia(valor_temporario);

De Resto looks OK, if it is not anything like that, I would need more information.

    
11.06.2018 / 07:38