java - How to generate ContextMenu Items Dynamically at run time? -
i'm creating music player app , want display names of available playlists when user want add song playlist, in contextmenu
.
so whole scenario is: when user long presses on song,
contextmenu
having 1 of options namedadd playlist
shown. when user clicks menu item,contextmenu
having optionscreate new playlist
, names of playlist available (if any) shown.
snap shot:
now problem is, show names of stored playlists, think have generate menu items dynamically @ run time. code of adding menu items is:
@override public void oncreatecontextmenu(contextmenu menu, view v, contextmenu.contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); if(contextmenuflag){ menu.setheadertitle(songtodisplay); menu.add(menu.none, play_song, menu.none, "play"); menu.add(menu.none, add_to_play_list, menu.none, "add playlist"); } else{ contextmenuflag = true; menu.setheadertitle("add playlist"); menu.add(menu.none, create_playlist, menu.none, "-create new playlist"); menu.add(menu.none, 10, menu.none, "old playlists if exists"); } //toast.maketext(mainactivity.this,v+"",toast.length_short).show(); }
here using loop, think can add menu items dynamically. problem in handling click events on menu items. code of event handling is:
@override public boolean oncontextitemselected(menuitem item) { switch (item.getitemid()) { case play_song: { songclick(myview); } break; case add_to_play_list: { contextmenuflag = false; songview.post(new runnable() { @override public void run() { songview.showcontextmenu(); } }); } break; case create_playlist:{ addtoplaylist(); } break; } return super.oncontextitemselected(item); }
here i'm using switch case each menu item id. question how handle click events in switch case if i've added menu items @ runtime?
please suggest alternatives if any. thank you.
by using statements following
menu.add(menu.none, 10, menu.none, "old playlists if exists");
you assign id value (here: 10) new menu item.
id values must positive integers. not have unique far android concerned want them unique within scope of context menu can evaluate them in switch
clause in oncontextitemselected(menuitem item)
.
in case, playlists have database id values (primary keys). can use them build menu item ids (let's add 100 have values < 100 left play_song, add_to_playlist etc.)
menu.add(menu.none, 100 + playlist_dbid, menu.none, myplaylistname);
this way, can evaluate item id in switch
clause , determine should done next.
@override public boolean oncontextitemselected(menuitem item) { switch (item.getitemid()) { case play_song: { songclick(myview); } break; case add_to_play_list: { contextmenuflag = false; songview.post(new runnable() { @override public void run() { songview.showcontextmenu(); } }); } break; case create_playlist:{ addtoplaylist(); } break; default: // user tapped on specific playlist // playlist database id reverting // step building item id int playlist_dbid = item.getitemid() - 100; dosomethingwithplaylist(playlist_dbid); } return super.oncontextitemselected(item); }
Comments
Post a Comment