如何动态地将SQLite数据传递给Android中的ArrayList?
问题描述:
我想动态地将所有数据从SQLite Table Aluno传递给ArrayList alunosinf,但它不工作。如何动态地将SQLite数据传递给Android中的ArrayList?
如果我尝试手动它的工作原理,例如:
alunolv student_Tom = new alunolv(123, "Tom Silva", "Avenue Tree", 91234567, "[email protected]");
alunolv student_Bob = new alunolv(321, "Bob Pereira", "Street One", 9876554, "[email protected]");
alunosinf.add(student_Tom);
alunosinf.add(student_Bob);
============================== ===========
我尝试SQLite的数据动态传递到ArrayList alunosinf:
SQLite的查询:
public Cursor mostraAlunos() {
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM Aluno", null);
return c;
}
MainActivity:
lv_Contactos = (ListView) findViewById(R.id.lvContactos);
List<alunolv> alunosinf = new ArrayList<alunolv>();
baseDeDados db;
public static final int numeroAluno = Integer.parseInt("numero");
public static final String nomeAluno = "nome";
public static final String moradaAluno = "morada";
public static final int telefoneAluno = Integer.parseInt("telefone");
public static final String emailAluno = "email";
Cursor alunos_cursor = db.mostraAlunos();
if (alunos_cursor != null && alunos_cursor.moveToFirst()) {
do {
int numeroNovo = alunos_cursor.getInt(alunos_cursor.getColumnIndex(String.valueOf(numeroAluno)));
String nomeNovo = alunos_cursor.getString(alunos_cursor.getColumnIndex(nomeAluno));
String moradaNova = alunos_cursor.getString(alunos_cursor.getColumnIndex(moradaAluno));
int telefoneNovo = alunos_cursor.getInt(alunos_cursor.getColumnIndex(String.valueOf(telefoneAluno)));
String emailNovo = alunos_cursor.getString(alunos_cursor.getColumnIndex(emailAluno));
alunolv aluno = new alunolv(numeroNovo, nomeNovo, moradaNova, telefoneNovo, emailNovo);
alunosinf.add(aluno);
} while (alunos_cursor.moveToNext());
}
ArrayAdapter<alunolv> adapter = new ArrayAdapter<alunolv>(this, android.R.layout.simple_list_item_1, alunosinf);
lv_Contactos.setAdapter(adapter);
构造:
public class alunolv {
int numero;
public int getNumero() {
return numero;
}
public void setNumero(int numero) {
this.numero = numero;
}
String nome;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
String morada;
public String getMorada() {
return morada;
}
public void setMorada(String morada) {
this.morada = morada;
}
int telefone;
public int getTelefone() {
return telefone;
}
public void setTelefone(int telefone) {
this.telefone = telefone;
}
String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public alunolv(int n, String no, String m, int t, String e){
numero = n;
nome = no;
morada = m;
telefone = t;
email = e;
}
@Override
public String toString() {
return nome;
}
public String ListarContacto(){
return String.valueOf(numero) + " " + nome + " " + morada + " " + String.valueOf(telefone) + " " + email;
}
}
谢谢!
答
使用Realm数据库而不是SQLite。你的生活将会简化。或者使用包装SQLite数据库的ORM。它减少了你的样板代码。
答
你混合了英文和葡萄牙文,所以很难理解。
但我的建议是,你创建一个方法来在数据库中做出选择并返回你的“Alunos”列表。
避免返回光标。您必须返回真正重要的内容(“Aluno”列表)。
见改善方法:
public List<alunolv> findAll() {
List<alunolv> list = new ArrayList<>();
String columns[] = new String[]{
"numero",
"nome",
"morada",
"telefone",
"email"};
Cursor cursor = db.query("Aluno", columns, null, null, null, null, null);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
do {
alunolv aluno = new alunolv();
aluno.setNumero(cursor.getInt(0));
aluno.setNome(cursor.getString(1));
aluno.setMorada(cursor.getString(2));
aluno.setTelefone(cursor.getInt(3));
aluno.setEmail(cursor.getString(4));
list.add(aluno);
} while (cursor.moveToNext());
}
return list;
}
+0
谢谢,但它不工作。它在单词查询中给出了错误(红色),而且我还有不在MainActivity中的SQLite mostraAlunos()。当我尝试删除返回c(光标)它会给出错误。 – Mrcoia
感谢,但对于这个项目我有使用SQLite。 在其他项目中,我会尝试使用其他选项。 – Mrcoia