我有一个数据库,需要更改以删除和添加值并创建这样一个ContextMenu,我有机会更改并删除单击的项目。 但是我需要知道列表项的id并且在contextMenu中,所以有菜单的id。 我正在做正确的事情,或者有更简单的方法。
public boolean onContextItemSelected(MenuItem item) {
//Iniciação da Base de dados
final HelperDB MyHelper = new HelperDB(this.getActivity());
SQLiteDatabase db = MyHelper.getWritableDatabase();
String[] projection = {Contrato_DB.CidadeEntry._ID, COLUMN_NOME,
};
// How you want the results sorted in the resulting Cursor
String sortOrder = COLUMN_NOME + " ASC ";
String[] columns = new String[] { "_id", "nome" };
final Cursor c = db.rawQuery("SELECT * FROM cidade ", null);
c.moveToFirst();
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.update: // <-- your custom menu item id here
int id = item.getItemId();
final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
final EditText input = new EditText(getActivity());
Toast.makeText(this.getActivity(),c.getString(tmp),Toast.LENGTH_LONG).show();
alert.setTitle("Editar :" + c.getString(tmp));
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString().trim();
答案 0 :(得分:0)
获取数据库ID,您只需执行此操作
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
long id = info.id;