I'm making an app to manage my tests at school and it's almost done. However, I have a problem: when I want the position (int position) of the ListView, it ALWAYS returns the first position, even if I select the second position, for example.
public class ProvasActivity extends ListActivity implements AdapterView.OnItemClickListener, DialogInterface.OnClickListener {
private List<Map<String, Object>> provas;
private AlertDialog dialogConfirmarExclusao;
private int provaSelecionada;
private Aplicativo dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.provas_activity);
dao = new Aplicativo(this);
android.app.ActionBar actionBar = getActionBar();
actionBar.setTitle("Minhas provas");
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
String[] de = {"materia", "conteudo"};
int[] para = {R.id.materia, R.id.conteudo};
SimpleAdapter adapter = new SimpleAdapter(this, listarProvas(), R.layout.provas_list_activity, de, para);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
registerForContextMenu(getListView());
this.dialogConfirmarExclusao = criarDialogConfirmarExclusao();
}
private List<Map<String, Object>> listarProvas() {
provas = new ArrayList<Map<String, Object>>();
List<Prova> listaProvas = dao.listarProvas();
for (Prova prova : listaProvas) {
Map<String, Object> item = new HashMap<String, Object>();
Long id = prova.getId();
String materia = prova.getMateria();
item.put("id", prova.getId());
item.put("materia", materia);
provas.add(item);
}
return provas;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Map<String, Object> map = provas.get(position);
String materia = (String) map.get("materia");
String msg = "Prova de " + materia;
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
provaSelecionada = position;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.provas_context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
Intent intent;
String id = String.valueOf(provas.get(provaSelecionada).get("id"));
switch (item.getItemId()) {
case R.id.remover:
dialogConfirmarExclusao.show();
break;
case R.id.editar:
intent = new Intent(this, NovaProvaActivity.class);
intent.putExtra(Constantes.PROVA_ID, id);
startActivity(intent);
return true;
}
return super.onContextItemSelected(item);
}
@Override
public void onClick(DialogInterface dialog, int item) {
Intent intent;
String id = String.valueOf(provas.get(provaSelecionada).get("id"));
switch (item) {
case DialogInterface.BUTTON_POSITIVE:
provas.remove(provaSelecionada);
dao.removerProva(id);
getListView().invalidateViews();
break;
case DialogInterface.BUTTON_NEGATIVE:
dialogConfirmarExclusao.dismiss();
break;
}
}
private AlertDialog criarDialogConfirmarExclusao() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.confirmar_exlusao_prova);
builder.setPositiveButton(getString(R.string.sim), (android.content.DialogInterface.OnClickListener) this);
builder.setNegativeButton(getString(R.string.nao), (android.content.DialogInterface.OnClickListener) this);
return builder.create();
}
@Override
protected void onDestroy() {
dao.close();
super.onDestroy();
}
}
How to solve?