1- Helper
2- Activity
3- Mensagem de erro: "Unfortunately, Agenda has stopped."
package com.example.peduboy.agenda;
import android.widget.EditText;
import android.widget.RatingBar;
import com.example.peduboy.agenda.modelo.Aluno;
public class FormularioHelper {
private final EditText camponome;
private final EditText campoendereço;
private final EditText campofone;
private final EditText camposite;
private final RatingBar camponota;
private Aluno aluno;
public FormularioHelper(FormularioActivity activity) {
camponome = (EditText) activity.findViewById(R.id.formulario_nome);
campoendereço = (EditText) activity.findViewById(R.id.formulario_endereço);
campofone = (EditText) activity.findViewById(R.id.formulario_telefone);
camposite = (EditText) activity.findViewById(R.id.formulario_site);
camponota = (RatingBar) activity.findViewById(R.id.formulario_nota);
aluno = new Aluno();
}
public Aluno pegaAluno() {
aluno.setNome(camponome.getText().toString());
aluno.setEndereço(campoendereço.getText().toString());
aluno.setTelefone(campofone.getText().toString());
aluno.setSite(camposite.getText().toString());
aluno.setNota(Double.valueOf(camponota.getProgress()));
return aluno;
}
public void preencheFormulario(Aluno aluno) {
camponome.setText(aluno.getNome());
campoendereço.setText(aluno.getEndereço());
campofone.setText(aluno.getTelefone());
camposite.setText(aluno.getSite());
camponota.setProgress(aluno.getNota().intValue());
this.aluno = aluno;
}
}
package com.example.peduboy.agenda;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.peduboy.agenda.dao.AlunoDAO;
import com.example.peduboy.agenda.modelo.Aluno;
public class FormularioActivity extends AppCompatActivity {
private FormularioHelper helper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_formulario);
Intent intent = getIntent();
Aluno aluno = (Aluno) intent.getSerializableExtra("aluno");
if (aluno != null) {
helper.preencheFormulario(aluno);
}
helper = new FormularioHelper(this);
}
@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 Salvo!", Toast.LENGTH_SHORT).show();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}