Get Item ID From Context Menu
hey people, I am trying to get the id of the item, in this case a table row, that was long pressed to bring up the context menu. This is my code so far.
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.delete:
deleteitem(id); //ID of item should be passed to method deleteitem
Toast.makeText(this, "delete",
Toast.LENGTH_LONG).show();
开发者_Python百科 return true;
default:
return super.onContextItemSelected(item);
}
}
As you can see I need the id of the table row to pass to another method. I have tried using info however it is always null. Obviously I am missing something here so hopefully you'll be able to point me in the right direction. Thanks.
Check the AdapterContextMenuInfo.id
field.
AdapterContextMenuInfo has both an id and position fields that correspond to the item in the adapter that was selected.
So in your code replace your deleteItem with this.
deleteItem(info.id);
should work.
精彩评论