Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

A lista de alunos não aparece na tela

tenho dúvida SQLiteDatabase ou SQLiteDataBase?

package com.example.hemp2.agenda.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.hemp2.agenda.modelo.Aluno;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hemp2 on 05/09/2017.
 */

public class AlunoDAO extends SQLiteOpenHelper {
    public AlunoDAO(Context context) {
        super(context, "Agenda", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE Alunos (id INTEGER PRIMARY KEY," +
                " nome TEXT NOT NULL, endereço TEXT, telefone TEXT, site TEXT, nota REAL);";
                db.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "DROP TABLE  IF EXISTS Alunos";
        db.execSQL(sql);
        onCreate(db);

    }


    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();
        Cursor c = db.rawQuery(sql, null);

        List<Aluno> alunos = new ArrayList<Aluno>();
        while (c.moveToNext()) {
            Aluno aluno = new Aluno();
            aluno.setId(c.getLong(c.getColumnIndex("id")));
            aluno.setNome(c.getString(c.getColumnIndex("nome")));
            aluno.setEndereco(c.getString(c.getColumnIndex("endereco")));
            aluno.setTelefone(c.getString(c.getColumnIndex("telefone")));
            aluno.setSite(c.getString(c.getColumnIndex("site")));
            aluno.setNota(c.getDouble(c.getColumnIndex("nota")));

            alunos.add(aluno);

        }
        c.close();

        return alunos;


    }
}
2 respostas

Também estou com o mesmo problema

É como se simplesmente os dados não fossem salvos

solução!

resolvido eu escrevi no sql errado "endereço ":)