我有一个与数据库链接的应用程序,我需要在按下后退按钮时打开一个菜单,其中包含保存、退出和取消选项。菜单已经创建,它可以工作(与列表视图一起使用),但我不知道如何将它与该onbackpressed
方法一起使用。有什么建议吗?
第一的:
registerForContextMenu(listview);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
openContextMenu(listview);
}
});
然后:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(R.string.MenuSospechosoQuestion);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_borrar_sospechoso, menu);
}
最后:
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.sospBorrar:
conex = new Conexion(this);
conex.conectar();
conex.actualizar("DELETE FROM SOSPECHOSOS WHERE ID_SOSPECHOSO = " + (ids.get(pos)));
ids.remove(pos);
names.remove(pos);
llenarLista();
conex.desconectar();
return true;
case R.id.sosEditar:
intent = new Intent(DenunciaMenuSuspect.this, Suspect.class);
intent.putExtra("toUpdate", true);
intent.putExtra("id", String.valueOf(ids.get(pos)));
startActivityForResult(intent, 1);
return true;
case R.id.cancelar:
return true;
default:
return super.onOptionsItemSelected(item);
}
}