I'm starting now on Android, I went to do a test of insertion of data in the bank, but the data is not inserted, I may be mistaken at some point, follow below the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
Class responsible for introducing students to the list:
AlunoDAO dao = new AlunoDAO(this);
List<Aluno> alunos = dao.buscaAlunos();
dao.close();
ListView listaAlunos = ( ListView) findViewById(R.id.lista_alunos);
ArrayAdapter<Aluno> adapter = new ArrayAdapter<Aluno>(this,android.R.layout.simple_list_item_1,alunos);
listaAlunos.setAdapter(adapter);
Button novoAluno = (Button) findViewById(R.id.novo_aluno);
Class responsible for entering and fetching students in the bank
public AlunoDAO(Context context) {
super(context, "Agenda",null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String sql = "CREATE TABLE Alunos (_id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, site TEXT, nota REAL);";
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS Alunos";
sqLiteDatabase.execSQL(sql);
onCreate(sqLiteDatabase);
}
public void insere(Aluno aluno) {
SQLiteDatabase db = getWritableDatabase();
ContentValues dados = new ContentValues();
dados.put("nome",aluno.getNome());
dados.put("endereco",aluno.getEndereco());
dados.put("telefone",aluno.getTelefone());
dados.put("site",aluno.getSite());
dados.put("nota",aluno.getNota());
db.insert("Alunos",null,dados);
}
public List<Aluno> buscaAlunos() {
String sql = "SELECT * FROM Alunos;";
SQLiteDatabase db = getReadableDatabase();//getReadableDatabase
Cursor c = db.rawQuery(sql,null);
List<Aluno> alunos = new ArrayList<Aluno>();
while(c.moveToNext()){
Aluno a = new Aluno();
a.setId(c.getLong(c.getColumnIndex("id")));
a.setNome(c.getString(c.getColumnIndex("nome")));
a.setEndereco(c.getString(c.getColumnIndex("endereco")));
a.setTelefone(c.getString(c.getColumnIndex("telefone")));
a.setSite(c.getString(c.getColumnIndex("site")));
a.setNota(c.getDouble(c.getColumnIndex("nota")));
alunos.add(a);
}
c.close();
return alunos;
}
No error is pointed out, using the debug always visualize that the array list returned by the students search is empty, the
db.insert("Alunos", null, dados);
I see by the debug that the completed data arrives.
I would like you to give me a light for this problem. Thanks in advance for your cooperation.