java - Remove items from different Lists with same Menu -
i have 2 listviews in activity , want use contextmenu remove itens 1 of them. want use same context menu, possible? looking here answers point 2 differents context menus. thanks!!!
here code:
//register both listviews listview1= (listview)findviewbyid(r.id.pedlstitens) ; listview1.setoncreatecontextmenulistener(this); registerforcontextmenu(listview1); listview2 = (listview)findviewbyid(r.id.pedlstcartao) ; listview2.setoncreatecontextmenulistener(this); registerforcontextmenu(listview2);
after that, inflate menu:
public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); menuinflater inflater = getmenuinflater(); inflater.inflate(r.layout.menu_remove, menu); }
then contextmenu behavior... don't know how point right listview remove item:
public boolean oncontextitemselected(menuitem item) { adaptercontextmenuinfo info = (adaptercontextmenuinfo) item.getmenuinfo(); ***//this dont know do*** switch (item.getitemid()) { case r.id.remove_item: listview1.remove(info.position); listview1array.notifydatasetchanged(); return true; } return false; }
found solution:
in "oncreatecontextmenu" stored id of list in variable listtoremove:
public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); menuinflater inflater = getmenuinflater(); listtoremove = v.getid(); //here store in listview contextmenu created inflater.inflate(r.layout.menu_remove, menu); }
then, in "oncontextitemselected", made switch control behavior. ***note: contextmenu has 1 item, removed "getitemid()" original code.
public boolean oncontextitemselected(menuitem item) { adaptercontextmenuinfo info = (adaptercontextmenuinfo) item.getmenuinfo(); boolean retorno = false; switch (listtoremove ) { case r.id.listview1: { listview1.remove(info.position); listview1array.notifydatasetchanged(); retorno = true; break; } case r.id.listview2: { listview2.remove(info.position); listview2array.notifydatasetchanged(); retorno = true; break; } } return retorno; }
works great!!!
Comments
Post a Comment