10
votes

In public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo} event ,

I think I can know which control launch onCreateContextMenu event by the arg View v using the following, right?
ImageView imageview=(ImageView)v

But In public boolean onContextItemSelected(MenuItem item), I can't find the same arg, how can I do? Thanks!

2

2 Answers

19
votes

You can use the ContextMenu.ContextMenuInfo like this:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    int index = info.position;
}

You can also get the exact View for which the menu is being displayed:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    int index = info.position;
    View view = info.targetView;
}

Look to these questions:

Android: How to find the position clicked from the context menu

Identifying the view selected in a ContextMenu (Android)

-1
votes

u can use

View v2;
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    v2=v;
      getMenuInflater().inflate(R.menu.cnt_menu, menu);
  //  }
  }