Não estou conseguindo deletar os alunos cadastrados e não consigo encontrar o erro.
package br.com.cadastro;
import java.util.List;
import br.com.cadastro.dao.AlunoDAO;
import br.com.cadastro.modelo.Aluno;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ListarAlunos extends Activity {
private ListView lista;
private Aluno aluno;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listar_alunos);
lista = (ListView) findViewById(R.id.lista);
registerForContextMenu(lista);
lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view,
int posicao, long id) {
Toast.makeText(ListarAlunos.this, "Clique posicao " + posicao,
Toast.LENGTH_SHORT).show();
}
});
lista.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter, View view,
int posicao, long id) {
aluno = (Aluno) adapter.getItemAtPosition(posicao);
return false;
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add("Ligar");
menu.add("Enviar SMS");
menu.add("Navegar no Site");
MenuItem deletar = menu.add("Deletar");
deletar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
AlunoDAO alunoDAO = new AlunoDAO(ListarAlunos.this);
alunoDAO.deletar(aluno);
alunoDAO.close();
carregaLista();
return false;
}
});
menu.add("Ver no Mapa");
menu.add("Enviar e-mail");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
protected void onResume() {
super.onResume();
carregaLista();
}
private void carregaLista() {
AlunoDAO dao = new AlunoDAO(this);
List<Aluno> alunos = dao.getLista();
dao.close();
int layout = android.R.layout.simple_list_item_1;
ArrayAdapter<Aluno> adapter = new ArrayAdapter<Aluno>(this, layout, alunos);
lista.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.listar_alunos, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemClicado = item.getItemId();
switch (itemClicado) {
case R.id.novo:
Intent proximoFormilario = new Intent(this, Formulario.class);
startActivity(proximoFormilario);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
classe DAO
package br.com.cadastro.dao;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import br.com.cadastro.modelo.Aluno;
public class AlunoDAO extends SQLiteOpenHelper{
private static final String DATABASE = "CadastroCaelum";
private static final int VERSAO = 2;
public AlunoDAO(Context context) {
super(context, DATABASE, null, VERSAO);
}
public void salva(Aluno aluno) {
ContentValues values = new ContentValues();
values.put("nome", aluno.getNome());
values.put("endereco", aluno.getEndereco());
values.put("telefone", aluno.getTelefone());
values.put("site", aluno.getSite());
values.put("nota", aluno.getNota());
values.put("foto", aluno.getFoto());
getWritableDatabase().insert("Alunos", null, values);
}
@Override
public void onCreate(SQLiteDatabase db) {
String ddl = "CREATE TABLE Alunos(id PRIMARY KEY, "+
"nome TEXT UNIQUE NOT NULL, telefone TEXT,"
+ "endereco TEXT, site TEXT, foto TEXT, nota REAL);";
db.execSQL(ddl);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String ddl = "DROP TABLE IF EXISTS Alunos";
db.execSQL(ddl);
this.onCreate(db);
}
public List<Aluno> getLista() {
String[] colunas = {"id,nome,endereco,telefone,site,foto,nota"};
Cursor cursor = getWritableDatabase().query("Alunos", colunas , null, null, null, null, null);
ArrayList<Aluno> alunos = new ArrayList<Aluno>();
while(cursor.moveToNext()){
Aluno aluno = new Aluno();
aluno.setId(cursor.getLong(0));
aluno.setNome(cursor.getString(1));
aluno.setEndereco(cursor.getString(2));
aluno.setTelefone(cursor.getString(3));
aluno.setSite(cursor.getString(4));
aluno.setFoto(cursor.getString(5));
aluno.setNota(cursor.getDouble(6));;
alunos.add(aluno);
}
return alunos;
}
public void deletar(Aluno aluno) {
String[] args = {aluno.getId().toString()};
getWritableDatabase().delete("Alunos", "id=?", args );
}
}