Android get parent id in multi context menu -
in code have listener have multi context menu :
button btn1= (button) findviewbyid(r.id.btn1); registerforcontextmenu(btn1); button edit_text1= (button) findviewbyid(r.id.edit_text1); registerforcontextmenu( edit_text1 );
now in oncontextitemselected
want witch widgets must text change. example:
@override public boolean oncontextitemselected(menuitem item) { adapterview.adaptercontextmenuinfo info = (adapterview.adaptercontextmenuinfo) item.getmenuinfo(); long buttonid = info.id; switch ( item.getitemid () ){ case 1: /* if user request context menu on btn1 */ btn1.settext( "ok" ); /* if user request context menu on edit_text1 */ edit_text1.settext( "" ); } return super.oncontextitemselected(item); }
unfortunately java.lang.nullpointerexception
error info.id
update post
public void oncreatecontextmenu(contextmenu menu, view v,contextmenu.contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); menu.setheadertitle( r.string.choose_selection ); view_id = v.getid(); menu.add(0, view_id, 0, getresources ().getstring ( r.string.clear ) ); menu.add(0, view_id, 0, getresources ().getstring ( r.string.cut ) ); menu.add(0, view_id, 0, getresources ().getstring ( r.string.copy ) ); } @override public boolean oncontextitemselected(menuitem item) { log.e("itemid ", item.getitemid ()+""); log.e("view_id ", view_id+""); switch ( item.getitemid () ){ case 1: break; } return super.oncontextitemselected(item); }
in oncontextitemselected
, item.getitemid ()
, view_id
same, after parent context menu how menu item id?
the getmenuinfo()
method return null in case, views not attaching info menu. saving classcastexception
, since views not child views of adapterview.
make buttonid
class member of activity, , retrieve view passed oncreatecontextmenu()
method.
private int buttonid; @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenu.contextmenuinfo menuinfo) { buttonid = v.getid(); ... }
Comments
Post a Comment