Não aparece o nome inserido no formulário
package br.com.alura.agenda;
import android.content.Intent;
import android.support.annotation.StringDef;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.List;
import br.com.alura.agenda.dao.AlunoDAO;
import br.com.alura.agenda.modelo.Aluno;
public class ListaAlunosActivity extends AppCompatActivity {
private ListView listaAlunos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
listaAlunos = (ListView) findViewById(R.id.lista_alunos);
Button novoAluno = (Button) findViewById(R.id.lista_aluno_novo);
novoAluno.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ListaAlunosActivity.this, FormularioActivity.class);
startActivity(intent);
}
});
}
public void carregaLista() {
AlunoDAO dao = new AlunoDAO(this);
List<Aluno> alunos = dao.buscaAlunos();
dao.close();
ArrayAdapter<Aluno> adapter = new ArrayAdapter<Aluno>(this, android.R.layout.simple_list_item_1, alunos);
listaAlunos.setAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
carregaLista();
}
}
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 java.util.ArrayList;
import java.util.List;
import br.com.alura.agenda.modelo.Aluno;
/**
* Created by RG on 12/08/2016.
*/
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, site TEXT, nota REAL);";db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXIST 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() {
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM Alunos;", 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;
}
}
package br.com.alura.agenda;
import android.widget.EditText;
import android.widget.RatingBar;
import java.util.Date;
import br.com.alura.agenda.modelo.Aluno;
//Esta classe manipula os dados do formulario
public class FormularioHelper {
//atributos guardar dados dos campos.
private EditText campoNome;
private EditText campoEndereco;
private EditText campoTelefone;
private EditText campoSite;
private RatingBar campoNota;
/*implementar construtor, responsavel guardar o conteudo do campo nos atributos passado
pela referencia activity
*/
public FormularioHelper (FormularioActivity activity){
this.campoNome = (EditText) activity.findViewById(R.id.formulario_nome);
this.campoEndereco = (EditText) activity.findViewById(R.id.formulario_endereco);
this.campoTelefone = (EditText) activity.findViewById(R.id.formulario_telefone);
this.campoSite = (EditText) activity.findViewById(R.id.formulario_site);
this.campoNota = (RatingBar) activity.findViewById(R.id.formulario_nota);
}
/*metodo que devolve um objeto do tipo aluno cosntruido com os dados preenchidos no formulario
* */
public Aluno pegaAluno (){
Aluno aluno = new Aluno();
aluno.setNome(campoNome.getText().toString());
aluno.setEndereco(campoEndereco.getText().toString());
aluno.setTelefone(campoTelefone.getText().toString());
aluno.setSite(campoSite.getText().toString());
aluno.setNota(Double.valueOf(campoNota.getProgress()));
return aluno;
}
}
package br.com.alura.agenda.modelo;
public class Aluno {
//atributos
private long id;
private String nome;
private String endereco;
private String telefone;
private String site;
private Double nota;
//getters e setters
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getSite() {
return site;
}
public void setSite(String site) {
this.site = site;
}
public Double getNota() {
return nota;
}
public void setNota(Double nota) {
this.nota = nota;
}
@Override
public String toString() {
return getId() + " - " + getNome();
}
}
package br.com.alura.agenda;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.widget.Button;
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);
//instanciar helper
this.helper = new FormularioHelper(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.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);
dao.insere(aluno);
dao.close();
Toast.makeText(FormularioActivity.this, "Aluno " + aluno.getNome() + " salvo!", Toast.LENGTH_SHORT).show();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}