views:

28

answers:

1

I'd like to show a ContextMenu for a custom View which is not part of an AdapterView.

I called

myActivity.registerForContextMenu(myView);

and the ContextMenu already shows up.

But I don't know how to access the data of myView in

@Override
onContextItemSelected(MenuItem item) //...

I thought I could create a custom menuInfo in

@Override
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) //...

This is from the docs: "menuInfo - Extra information about the item for which the context menu should be shown. This information will vary depending on the class of v."

Can anyone give me a hint?

+1  A: 

Your custom view class should override getContextMenuInfo() and return a custom object implementing ContextMenuInfo (not very hard considering the interface has no members!) and which contains the extra per-view data you want the menu-click handler to get.

You can then get at this object from onContextItemSelected(MenuItem item) by calling item.getMenuInfo() and casting the value returned.

Reuben Scratton
Alright, thanks! That helped me a lot :)
cody