Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get View in OnContextItemSelected event?

Tags:

android

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!

like image 706
HelloCW Avatar asked Jul 17 '13 00:07

HelloCW


1 Answers

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)

like image 193
hasanghaforian Avatar answered Nov 10 '22 02:11

hasanghaforian