I'm creating dynamically Button views with a context menu. When a context menu item is selected, I would like to retrive the Button view which triggered the context menu.
This is how I create the button :
// Create a new button
Button buttonView = new Button(this);
// Set button text
buttonView.setText("MyButton");
// Set on click listener
buttonView.setOnClickListener( new ButtonClickHandler() );
// Register for context menu
registerForContextMenu(buttonView);
This is how I create the context menu :
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
And this is how I handle selected items :
public boolean onContextItemSelected(MenuItem item) {
// Get extra menu information about the item selected
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
//
switch (item.getItemId()) {
case R.id.delete:
// Retrieve selected button text
String btnText = ((Button) info.targetView).getText().toString();
// etc...
return true;
default:
return super.onContextItemSelected(item);
}
}
The issue is that "(AdapterContextMenuInfo) item.getMenuInfo()" returns null, ie there is no extra information about the item. I was expecting to get the Button view in info.targetView. Apparently this works only for ListView, because AdapterView takes care of populating this extra info.
I guess I should do something in "onCreateContextMenu" to attach this information. A sample code to attach this information would be very much welcomed.
Thanks
用于创建上下文菜单的Button
是传递给onCreateContextMenu
的View
参数。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.