android - How to get position of clicked listview in single clicked contextmenu -
i have defined listview in xml , add data modifying through adapter.. want display context menu single click list, done.. how clicked list position ?
here code, how call context menu :
list = (listview)findviewbyid(r.id.grant_list); // data in database list<grantlistdata> datalist = con.getalllist(); try { list.removeallviews(); } catch (exception e) {} listall.clear(); arrname.clear(); arrphone.clear(); (int = 0; < datalist.size(); ++i) { listall.add(datalist.get(i)); arrname.add(datalist.get(i).getname().tostring()); arrphone.add(datalist.get(i).getphone().tostring()); } adapter = new stablearrayadapter(this,android.r.layout.simple_list_item_1, arrname); list.setadapter(adapter); this.registerforcontextmenu(list); list.setonlongclicklistener(this); list.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int pos, long id) { string item = (string)parent.getitematposition(pos); activity.registerforcontextmenu(view); activity.opencontextmenu(view); } });
and oncreatecontextmenu method :
int num=-1; @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); adaptercontextmenuinfo info = (adaptercontextmenuinfo) menuinfo; //string clicked = adapter.getitem(info.position); --> when confused //num = info.position; //menu.setheadertitle("options " + clicked); menu.add(1, 1, 1, "details"); //menu.addsubmenu(1, 1, 1, arrname.get(info.position)); //menu.addsubmenu(1, 1, 2, arrphone.get(info.position)); menu.add(1, 2, 2, "delete"); }
[addition]
private class stablearrayadapter extends arrayadapter<string> { hashmap<string, integer> midmap = new hashmap<string, integer>(); public stablearrayadapter(context context, int textviewresourceid, list<string> objects) { super(context, textviewresourceid, objects); (int = 0; < objects.size(); ++i) { midmap.put(objects.get(i), i); } } @override public long getitemid(int position) { string item = getitem(position); return midmap.get(item); } @override public boolean hasstableids() { return true; } }
thanks in advance :)
[solved] well, took while me realize simple thing. mean
list.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int pos, long id) { currlist = (string)parent.getitematposition(pos); currindex = pos; registerforcontextmenu(view); opencontextmenu(view); } });
and
int num=-1; @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); adaptercontextmenuinfo info = (adaptercontextmenuinfo) menuinfo; menu.setheadertitle("options "+ currlist); menu.add(1, 1, 1, "details"); menu.addsubmenu(1, 1, 1, arrname.get(currindex)); menu.addsubmenu(1, 1, 2, arrphone.get(currindex)); menu.add(1, 2, 2, "delete"); }
thanks answers anw :)
you set tag position view , set groupid menu item position.
list.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int pos, long id) { string item = (string)parent.getitematposition(pos); view.settag(position); activity.registerforcontextmenu(view); activity.opencontextmenu(view); } });
put position in groupid of menu item can retrieve getgroupid
@override public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); adaptercontextmenuinfo info = (adaptercontextmenuinfo) menuinfo; //string clicked = adapter.getitem(info.position); --> when confused int position = integer.parseint(v.gettag()); //num = info.position; //menu.setheadertitle("options " + clicked); menu.add(position, 1, 1, "details"); //menu.addsubmenu(1, 1, 1, arrname.get(info.position)); //menu.addsubmenu(1, 1, 2, arrphone.get(info.position)); menu.add(position, 2, 2, "delete"); }
Comments
Post a Comment