Pregunta

Im working on a simple music player for my intro to Android apps class. I want to be able to add songs to a playlist listed in my context menu. When I click on my addtoplaylist contextmenuitem I want my popup menu to appear. How do I call my popup menu? Also if you have some suggestions on how to populate my popup menu, rather than my for loop, that would be cool too.

I have a contextmenu listener that kind of looks like this.

    @Override
public boolean onContextItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.share:
            shareIt();
            return true;
        case R.id.Store:
            musicStore();
            return true;
        case R.id.addtoplaylist:
        // open popup menu
            return true;
        case R.id.snippet:
            snippet(tem1);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}    

And i have a popup menu that kind of looks like this.

        public void showPopup(View v) {

    int i = view.getPlaylists().size();
    ArrayList<String> playlist = view.getPlaylists();

    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.popup_menu, popup.getMenu());
    for(int k = 0; k > i;k++){
        popup.getMenu().add(playlist.get(k));
    }

    popup.show();
}
¿Fue útil?

Solución

As you can see here PopupMenu V is just an anchor, so you can pass for instance the ListView which contains your item Or if you really want the popupmenu to be anchored to the item have a look here

You get your target view like this.

AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 
View  v = info.targetView;

So after call

showPopUp(v);

For your loop, I don't see a far better solution.

Otros consejos

the accepted question is fine, but for the for loop part you could use an iterator :)

for (String song : playlist) {
    popup.getMenu().add(song);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top