Sqlite result return wrong column values different from database

I’m trying to make a select query in my android app to my local sqlite database and in my database I have the following result like

Id  IdVisitaParque Parque TipoParque TipoEstabulacao Animais Amostra Bebedouros CriadoPor CriadoA
1   422326          1       1            2             53      29        3      1
2   422327          2       2            1              2       2        3      1
3   443935          3       1            1             14       8        4      3

But with the following code,

SQLiteDatabase db = dbh.getWritableDatabase();
ArrayList<Parques> parques = new ArrayList<Parques>();
    
String query = "SELECT * FROM parques";
Cursor cursor = null;
try{
    cursor = db.rawQuery(query, null);
    if(cursor.moveToFirst()){
       while(!cursor.isAfterLast()){
            Parques p = new Parques();
            p.setIdVisitaParque(cursor.getColumnIndex(columnIdVisitaParque));
            p.setNumParque(cursor.getColumnIndex(columnNumParque));
            p.setTipoParque(cursor.getColumnIndex(columnTipoParque));
            p.setTipoEstabulacao(cursor.getColumnIndex(columnTipoEstabulacao));
            p.setNumAnimais(cursor.getColumnIndex(columnNumAnimais));
            p.setAmostra(cursor.getColumnIndex(columnAmostra));
            p.setNumBebedouros(cursor.getColumnIndex(columnNumbebedouros));
            p.setCriadoPor(cursor.getColumnIndex(columnCriadoPor));
            parques.add(p);
            cursor.moveToNext();
        };
    }
}catch (SQLException e){
    e.printStackTrace();
}
finally {
    cursor.close();
    db.close();
}

I receive 3 lines with the same result,

amostra = 6
arLivreDiasAno = 0
arLivreHorasDia = 0
criadoPor = 9
descricaoParque = 0
idVisitaParque = 1
numAnimais = 5
numBebedouros = 7
numParque = 2
pastoreioDiasAno = 0
pastoreioHorasDia = 0
tipoEstabulacao = 4
tipoParque = 3

I can’t understand this.

The creation in sqlite table,

public static final String tableName = "parques";
    public static final String columnId = "Id";
    public static final String columnIdVisitaParque = "IdVisitaParque";
    public static final String columnNumParque = "Parque";
    public static final String columnTipoParque = "TipoParque";
    public static final String columnTipoEstabulacao = "TipoEstabulacao";
    public static final String columnNumAnimais = "Animais";
    public static final String columnAmostra = "Amostra";
    public static final String columnNumbebedouros = "Bebedouros";
    public static final String columnCriadoA = "CriadoA";
    public static final String columnCriadoPor = "CriadoPor";

    final DatabaseHelper dbh;

    public static final String createTable = "CREATE TABLE " + tableName + "("
            + columnId + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + columnIdVisitaParque + " INTEGER,"
            + columnNumParque + " INTEGER, "
            + columnTipoParque + " INTEGER, "
            + columnTipoEstabulacao + " INTEGER, "
            + columnNumAnimais + " INTEGER, "
            + columnAmostra + " INTEGER, "
            + columnNumbebedouros + " INTEGER, "
            + columnCriadoA + " DATETIME,"
            + columnCriadoPor + " INTEGER"
            + ")";

Answer

You’re updating the created p object with the column indexes, not their values. Instead of using the value of getColumnIndex, you should use it to get the actual value, e.g., with getInt:

while(!cursor.isAfterLast()){
    Parques p = new Parques();
    p.setIdVisitaParque(cursor.getInt(cursor.getColumnIndex(columnIdVisitaParque)));
    p.setNumParque(cursor.getInt(cursor.getColumnIndex(columnNumParque)));
    p.setTipoParque(cursor.getInt(cursor.getColumnIndex(columnTipoParque)));
    p.setTipoEstabulacao(cursor.getInt(cursor.getColumnIndex(columnTipoEstabulacao)));
    p.setNumAnimais(cursor.getInt(cursor.getColumnIndex(columnNumAnimais)));
    p.setAmostra(cursor.getInt(cursor.getColumnIndex(columnAmostra)));
    p.setNumBebedouros(cursor.getInt(cursor.getColumnIndex(columnNumbebedouros)));
    p.setCriadoPor(cursor.getInt(cursor.getColumnIndex(columnCriadoPor)));
    parques.add(p);
    cursor.moveToNext();
};