java - How can add popupMenu to items of listView in Android? -
i want show popupmenu onlongclick in android, write:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); listview = (listview) findviewbyid(r.id.listview); adapter = new myadapter(getapplicationcontext()); } @override protected void onresume() { super.onresume(); connecttodatabase(); } private void connecttodatabase() { //create database listview.setadapter(adapter); }
my adapter:
@override public view getview(final int position, view convertview, final viewgroup parent) { final myholder holder; if (convertview == null) { convertview = layoutinflater.inflate(r.layout.list_row, null); holder = new myholder(); holder.vocabs = (textview) convertview.findviewbyid(r.id.textviewvocabs); holder.points = (textview) convertview.findviewbyid(r.id.textviewpoints); holder.tick = (imagebutton) convertview.findviewbyid(r.id.tick); holder.cross = (imagebutton) convertview.findviewbyid(r.id.cross); convertview.settag(holder); } else { holder = (myholder) convertview.gettag(); } holder.id = id.get(position); holder.vocabs.settext(vocabs.get(position)); holder.points.settext(points.get(position) + ""); holder.tick.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //toast.maketext(parent.getcontext(), "yes... :d ... learn :d", toast.length_short).show(); points.set(position, points.get(position) + 1); database.execsql("update vocabs set points=" + points.get(position) + " id=" + id.get(position) + ";"); notifydatasetchanged(); } }); holder.cross.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //toast.maketext(parent.getcontext(), "yes... :d ... learn :d", toast.length_short).show(); if (points.get(position) != 0) { points.set(position, points.get(position) - 1); database.execsql("update vocabs set points=" + points.get(position) + " id=" + id.get(position) + ";"); } notifydatasetchanged(); } }); convertview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(parent.getcontext(), means.get(position), toast.length_long).show(); } }); final view finalconvertview = convertview; convertview.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { //popup-menu delete item. popupmenu popupmenu = new popupmenu(parent.getcontext(),finalconvertview); popupmenu.getmenuinflater().inflate(r.menu.popup_menu, popupmenu.getmenu()); popupmenu.show(); popupmenu.setonmenuitemclicklistener(new popupmenu.onmenuitemclicklistener() { @override public boolean onmenuitemclick(menuitem item) { if (item.gettitle().equals("delete")) { database.execsql("delete vocabs id=" + holder.id + ";"); id.remove(position); vocabs.remove(position); means.remove(position); points.remove(position); notifydatasetchanged(); } return false; } }); //toast.maketext(parent.getcontext(), "onlongclickfunction! " + position, toast.length_long).show(); return false; } }); return convertview; }
i work itellij idea 13.1.4. add appcompat supports popup_menu on apis older 11. when run application, everythings ok. when long click on item in listview below error:
04-01 15:50:29.749 17078-17078/net.motameni.apps.vocabs_box e/androidruntime﹕ fatal exception: main process: net.motameni.apps.vocabs_box, pid: 17078 java.lang.runtimeexception: failed resolve attribute @ index 6 @ android.content.res.typedarray.getlayoutdimension(typedarray.java:603) @ android.view.viewgroup$layoutparams.setbaseattributes(viewgroup.java:6423) @ android.view.viewgroup$marginlayoutparams.<init>(viewgroup.java:6591) @ android.widget.framelayout$layoutparams.<init>(framelayout.java:735) @ android.widget.framelayout.generatelayoutparams(framelayout.java:679) @ android.widget.framelayout.generatelayoutparams(framelayout.java:62) @ android.view.layoutinflater.inflate(layoutinflater.java:492) @ android.view.layoutinflater.inflate(layoutinflater.java:414) @ android.support.v7.internal.view.menu.menupopuphelper$menuadapter.getview(menupopuphelper.java:370) @ android.support.v7.internal.view.menu.menupopuphelper.measurecontentwidth(menupopuphelper.java:219) @ android.support.v7.internal.view.menu.menupopuphelper.tryshow(menupopuphelper.java:153) @ android.support.v7.internal.view.menu.menupopuphelper.show(menupopuphelper.java:125) @ android.support.v7.widget.popupmenu.show(popupmenu.java:193) @ net.motameni.apps.vocabs_box.mainactivity$myadapter$4.onlongclick(mainactivity.java:154) @ android.view.view.performlongclick(view.java:4795) @ android.view.view$checkforlongpress.run(view.java:19723) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
what wrong???
i suspect you're not getting correct view when calling popupmenu constructor.
change code from:
popupmenu popupmenu = new popupmenu(parent.getcontext(),finalconvertview);
to:
popupmenu popupmenu = new popupmenu(parent.getcontext(), v);
i admit maybe same view depending on layout design. suggested code under more accurate. view parameter v points row item in listview.
please post menu xml r.menu.popup_menu also.
Comments
Post a Comment