// ListaAlunosActivity
package br.com.the7web.cadastro;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.Serializable;
import java.util.List;
/**
* Created by J r n i n on 16/11/2015.
*/
public class ListaAlunosActivity extends AppCompatActivity {
private ListView lista;
private Aluno aluno;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listagem_alunos);
lista= (ListView) findViewById(R.id.lista);
registerForContextMenu(lista);
lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int posicao, long id) {
Aluno alunoParaSerAlterado = (Aluno) adapter.getItemAtPosition(posicao);
Intent irParaFormulario = new Intent(ListaAlunosActivity.this, br.com.the7web.cadastro.FormularioActivity.class);
irParaFormulario.putExtra("alunoSelecionado", (Serializable) alunoParaSerAlterado);
startActivity(irParaFormulario);
}
});
lista.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter, View view, int posicao, long id) {
aluno = (Aluno) adapter.getItemAtPosition(posicao);
return false;
}
});
}
@Override
protected void onResume(){
super.onResume();
carregaLista();
}
private void carregaLista() {
AlunoDAO dao = new AlunoDAO(this);
List<Aluno> alunos = dao.getLista();
ArrayAdapter<Aluno> adapter = new ArrayAdapter<Aluno>(this, android.R.layout.simple_list_item_1, alunos);
lista.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_lista_alunos, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.novo:
Intent irParaFormulario = new Intent(this, br.com.the7web.cadastro.FormularioActivity.class);
startActivity(irParaFormulario);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add("Ligar");
menu.add("Enviar SMS");
menu.add("Achar no Mapa");
menu.add("Navegar no Site");
MenuItem deletar = menu.add("Deletar");
deletar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
AlunoDAO dao = new AlunoDAO(ListaAlunosActivity.this);
dao.deletar(aluno);
dao.close();
carregaLista();
return false;
}
});
menu.add("Enviar E-mail");
super.onCreateContextMenu(menu, v, menuInfo);
}
}
// AlunoDAO
package br.com.the7web.cadastro;
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;
/**
* Created by J r n i n on 17/11/2015.
*/
class AlunoDAO extends SQLiteOpenHelper {
private static final String DATABASE = "NomeDoBanco";
private static final int VERSAO = 2;
private static final String TABELA = "Alunos";
public AlunoDAO(Context ctx){
super(ctx, DATABASE, null, VERSAO);
}
@Override
public void onCreate(SQLiteDatabase database) {
String sql = "CREATE TABLE " + TABELA + " ("
+ "id INTEGER PRIMARY KEY, "
+ "nome TEXT UNIQUE NOT NULL, "
+ "telefone TEXT, "
+ "endereco TEXT, "
+ "site TEXT, "
+ "nota REAL, "
+ "caminhoFoto TEXT"
+ ");";
database.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS " + TABELA;
database.execSQL(sql);
onCreate(database);
}
public void salvar(Aluno aluno){
ContentValues values = new ContentValues();
values.put("nome", aluno.getNome());
values.put("endereco", aluno.getEndereco());
values.put("site", aluno.getSite());
values.put("telefone", aluno.getTelefone());
values.put("nota", aluno.getNota());
values.put("caminhoFoto", aluno.getCaminhoFoto());
getWritableDatabase().insert("Alunos", null, values);
}
public void insere(Aluno aluno) {
//informando para o bd onde queremos colocar cada informação nas colunas do bd
ContentValues cv = new ContentValues();
cv.put("nome", aluno.getNome());
cv.put("telefone", aluno.getTelefone());
cv.put("endereco", aluno.getEndereco());
cv.put("site", aluno.getSite());
cv.put("nota", aluno.getNota());
cv.put("caminhoFoto", aluno.getCaminhoFoto());
getWritableDatabase().insert(TABELA, null, cv);
}
public List<Aluno> getLista() {
List<Aluno> alunos = new ArrayList<Aluno>();
String sql = "SELECT * FROM " + TABELA + ";";
Cursor c = getReadableDatabase().rawQuery(sql, null);
while (c.moveToNext()){
Aluno aluno = new Aluno();
aluno.setId(c.getLong(c.getColumnIndex("id")));
aluno.setNome(c.getString(c.getColumnIndex("nome")));
aluno.setTelefone(c.getString(c.getColumnIndex("telefone")));
aluno.setEndereco(c.getString(c.getColumnIndex("endereco")));
aluno.setSite(c.getString(c.getColumnIndex("site")));
aluno.setNota(c.getDouble(c.getColumnIndex("nota")));
aluno.setCaminhoFoto(c.getString(c.getColumnIndex("caminhoFoto")));
alunos.add(aluno);
}
return null;
}
public void deletar(Aluno aluno) {
String[] args = {aluno.getId().toString()};
getWritableDatabase().delete("Alunos", "id=?", args );
}
public void atualizar(Aluno aluno) {
ContentValues values = new ContentValues();
values.put("nome", aluno.getNome());
values.put("endereco", aluno.getEndereco());
values.put("site", aluno.getSite());
values.put("telefone", aluno.getTelefone());
values.put("nota", aluno.getNota());
values.put("caminhoFoto", aluno.getCaminhoFoto());
String[] args = {aluno.getId().toString()};
getWritableDatabase().update("Alunos", values, "id=?", args);
}
}