android - Context menu does not show in long click even of listview -
i have listview. want show context menu when press long click in item. , context menu show "delete" , "edit". implemented below code. however, not show context menu when press long click item. note that, long click item implemented successfully, however, not show context menu. fix me?
public class manageactivity extends activity { private listview mainlistview ; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.list_main); // find listview resource. mainlistview = (listview) findviewbyid( r.id.mainlistview ); mainlistview.setadapter( listadapter ); registerforcontextmenu(mainlistview); } mainlistview.setonitemlongclicklistener(new android.widget.adapterview.onitemlongclicklistener() { public boolean onitemlongclick(adapterview<?> arg0, view arg1, int pos, long id) { // todo auto-generated method stub string audio_file_name = (string) mainlistview.getitematposition(pos); log.v("long clicked",string.valueof(audio_file_name)); return true; } }); } @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo){ menu.add(0, v.getid(), 0, "edit"); menu.add(0, v.getid(), 0, "delete"); } @override public boolean oncontextitemselected(menuitem item){ if(item.gettitle()=="edit")toast.maketext(getapplicationcontext(), "edit clicked", toast.length_long).show(); if(item.gettitle()=="delete")toast.maketext(getapplicationcontext(), "delete clicked", toast.length_long).show(); return true; }
update log
09-30 15:24:35.611: e/androidruntime(26204): fatal exception: main 09-30 15:24:35.611: e/androidruntime(26204): java.lang.nullpointerexception 09-30 15:24:35.611: e/androidruntime(26204): @ com.example.test.manageactivity.oncontextitemselected(manageactivity.java:91) 09-30 15:24:35.611: e/androidruntime(26204): @ android.app.activity.onmenuitemselected(activity.java:2728) 09-30 15:24:35.611: e/androidruntime(26204): @ com.android.internal.policy.impl.phonewindow$dialogmenucallback.onmenuitemselected(phonewindow.java:4342) 09-30 15:24:35.611: e/androidruntime(26204): @ com.android.internal.view.menu.menubuilder.dispatchmenuitemselected(menubuilder.java:735) 09-30 15:24:35.611: e/androidruntime(26204): @ com.android.internal.view.menu.menuitemimpl.invoke(menuitemimpl.java:152) 09-30 15:24:35.611: e/androidruntime(26204): @ com.android.internal.view.menu.menubuilder.performitemaction(menubuilder.java:874) 09-30 15:24:35.611: e/androidruntime(26204): @ com.android.internal.view.menu.menudialoghelper.onclick(menudialoghelper.java:204) 09-30 15:24:35.611: e/androidruntime(26204): @ com.android.internal.app.alertcontroller$alertparams$3.onitemclick(alertcontroller.java:994) 09-30 15:24:35.611: e/androidruntime(26204): @ android.widget.adapterview.performitemclick(adapterview.java:308) 09-30 15:24:35.611: e/androidruntime(26204): @ android.widget.abslistview.performitemclick(abslistview.java:1509) 09-30 15:24:35.611: e/androidruntime(26204): @ android.widget.abslistview$performclick.run(abslistview.java:3471) 09-30 15:24:35.611: e/androidruntime(26204): @ android.widget.abslistview$3.run(abslistview.java:4834) 09-30 15:24:35.611: e/androidruntime(26204): @ android.os.handler.handlecallback(handler.java:733) 09-30 15:24:35.611: e/androidruntime(26204): @ android.os.handler.dispatchmessage(handler.java:95) 09-30 15:24:35.611: e/androidruntime(26204): @ android.os.looper.loop(looper.java:146) 09-30 15:24:35.611: e/androidruntime(26204): @ android.app.activitythread.main(activitythread.java:5602) 09-30 15:24:35.611: e/androidruntime(26204): @ java.lang.reflect.method.invokenative(native method) 09-30 15:24:35.611: e/androidruntime(26204): @ java.lang.reflect.method.invoke(method.java:515) 09-30 15:24:35.611: e/androidruntime(26204): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1283) 09-30 15:24:35.611: e/androidruntime(26204): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1099) 09-30 15:24:35.611: e/androidruntime(26204): @ dalvik.system.nativestart.main(native method)
try this
@override public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo){ menu.add(v.getid(), 1, 0, "edit"); menu.add(v.getid(), 2, 0, "delete"); }
and oncontextitemselected()
method should this.
@override public boolean oncontextitemselected(menuitem item) { adapterview.adaptercontextmenuinfo acmi = (adaptercontextmenuinfo) item.getmenuinfo(); string audio_file_namej = (string) mainlistview.getitematposition(acmi.position); log.v("long clicked",string.valueof(audio_file_name)); switch (item.getitemid()) { case 1: toast.maketext(getapplicationcontext(), "edit clicked", toast.length_long).show(); break; case 2: toast.maketext(getapplicationcontext(), "delete clicked", toast.length_long).show(); break; } return true; }
Comments
Post a Comment