Eu insiro os dados no aplicativo agenda, mas ao reiniciar a lista nao popula.
Aparece o erro no android:
android.database.sqlite.SQLiteConstraintException: Alunos.TEXT may not be NULL (code 19)
Erro completo:
E/SQLiteDatabase: Error inserting site=alura.com nome=Rodrigo nota=6.0 endereco=Rua alguma coisa telefone=123457
android.database.sqlite.SQLiteConstraintException: Alunos.TEXT may not be NULL (code 19)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:791)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:926)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1570)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1440)
at br.com.rodrigo.agendinha.dao.AlunoDAO.insere(AlunoDAO.java:51)
at br.com.rodrigo.agendinha.FormularioActivity.onOptionsItemSelected(FormularioActivity.java:45)
at android.app.Activity.onMenuItemSelected(Activity.java:2644)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:421)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:188)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103)
at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:663)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:947)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:155)
at android.view.View.performClick(View.java:4461)
at android.view.View$PerformClick.run(View.java:18525)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
//Classe AlunoDAO:
package br.com.rodrigo.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 android.support.annotation.NonNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import br.com.rodrigo.agenda.modelo.Aluno;
/**
* Created by Rodrigo on 8/7/2016.
*/
public class AlunoDAO extends SQLiteOpenHelper {
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, telefone TEXT, site TEXT, nota REAL);";
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
String sql = "DROP TABLE IF EXISTS Alunos";
sqLiteDatabase.execSQL(sql);
onCreate(sqLiteDatabase);
}
public void insere(Aluno aluno) {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues dados = new ContentValues();
//dados.put("id", aluno.getId());
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());
sqLiteDatabase.insert("Alunos", null, dados);
}
public List<Aluno> buscaAlunos() {
String sql = "SELECT * FROM Alunos;";
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
Cursor c = sqLiteDatabase.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 aluno;
}