Ao tentar inserir um novo aluno, no memento de salvar o aplicativo para, só consigo editar existentes.
package br.com.alura.agenda;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import br.com.alura.agenda.dao.AlunoDAO;
import br.com.alura.agenda.modelo.Aluno;
public class FormularioActivity extends AppCompatActivity {
private FormularioHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_formulario);
helper = new FormularioHelper(this);
Intent intent = getIntent();
Aluno aluno = (Aluno) intent.getSerializableExtra("aluno");
if (aluno != null) {
helper.preencheFormulario(aluno);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_formulario, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_formulario_ok:
Aluno aluno = helper.pegaAluno();
AlunoDAO dao = new AlunoDAO(this);
if (aluno.getId() != null){
dao.altera(aluno);
} else {
dao.insere(aluno);
}
dao.close();
Toast.makeText(FormularioActivity.this, "Aluno " + aluno.getNome() + " salvo!", Toast.LENGTH_SHORT).show();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
package br.com.alura.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.List;
import br.com.alura.agenda.modelo.Aluno;
/**
* Created by julio.maggieri on 24/01/2018.
*/
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, endereco 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 = pegaDadosDoAluno(aluno);
db.insert("Alunos", null, dados);
}
@NonNull
private ContentValues pegaDadosDoAluno(Aluno aluno) {
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());
return dados;
}
public List<Aluno> buscaAlunos() {
String sql = "SELECT * from Alunos;";
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery(sql, null);
List<Aluno> alunos = new ArrayList<>();
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;
}
public void deleta(Aluno aluno) {
SQLiteDatabase db = getWritableDatabase();
String[] params = {aluno.getId().toString()};
db.delete("Alunos", "id = ?", params);
}
public void altera(Aluno aluno) {
SQLiteDatabase db = getWritableDatabase();
ContentValues dados = pegaDadosDoAluno(aluno);
String[] params = {aluno.getId().toString()};
db.update("Alunos", dados, "id = ?", params);
}
}