I am trying to create a List of objects in Android (java) from a query in a SQLite database. The code is the following:
public List<Aluno> buscaAlunos() {
String sql = "SELECT * FROM ALUNOS";
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery(sql, null);
List<Aluno> alunos = new ArrayList<Aluno>();
Aluno aluno = null;
while (c.moveToNext()) {
aluno = new Aluno();//new using same object aways
aluno.setId(c.getLong(c.getColumnIndex("ID")));
aluno.set_nome(c.getString(c.getColumnIndex("NOME")));
aluno.set_endereco(c.getString(c.getColumnIndex("ENDERECO")));
aluno.set_telefone(c.getString(c.getColumnIndex("TELEFONE")));
aluno.set_site(c.getString(c.getColumnIndex("SITE")));
aluno.set_nota(c.getFloat(c.getColumnIndex("NOME")));
alunos.add(aluno);
}
c.close();
return alunos;
}
When I try to debug, I get this information on the beginning of the second iteraction(note: I've overwritten Aluno.toString() to show some information in the object): first print
And then, when I go a few steps in, I can see the same object beeing altered (look at the ArrayList alunos):second print
The result of that code is a list with all the objects in the list having the same value.
Thanks in advance for all the help. EDIT: Code for Aluno
package com.muffinalunos.muffinmad.muffinalunos.modelo;
public class Aluno {
private static long _ID;
private static String _nome;
private static String _endereco;
private static String _telefone;
private static String _site;
private static float _nota;
public Aluno(){
}
public static long getId() {
return _ID;
}
public static void setId(long Id) {
_ID = Id;
}
public static String get_nome() {
return _nome;
}
public static void set_nome(String _nome) {
Aluno._nome = _nome;
}
public static String get_endereco() {
return _endereco;
}
public static void set_endereco(String _endereco) {
Aluno._endereco = _endereco;
}
public static String get_telefone() {
return _telefone;
}
public static void set_telefone(String _telefone) {
Aluno._telefone = _telefone;
}
public static String get_site() {
return _site;
}
public static void set_site(String _site) {
Aluno._site = _site;
}
public static float get_nota() {
return _nota;
}
public static void set_nota(float _nota) {
Aluno._nota = _nota;
}
@Override
public String toString() {
return getId()+" - "+get_nome();
}
}