显示列表项的上下文菜单

时间:2012-03-22 08:06:53

标签: android contextmenu onlongclicklistener

我正在开发Android音乐播放器。下面的代码用于显示歌曲列表,并在列表中长按特定歌曲时显示一个对话框。但不知何故,它在这种情况下不起作用。你能解决问题吗? ?

public class ListQueue extends ListActivity{
//  ListActivity l1= new ListActivity();
    public static final String MEDIA_PATH= new String("/sdcard/");  //path of the songs
    public static List<String> songs = new ArrayList<String>(); //array name of the songs
    public static int currentPosition = 0;  //current position of the song in the List
    public static String songName;          //name of the song
    public static int duration;         //total duration of the song
    public static MediaPlayer mp=new MediaPlayer();
    public String[] menuItems={"set as ringtone","delete","send","Crop","details"};
    public String[] songsarray;
    public int i=0;
    //  public TextView textViewToChange=null;
    //  public static double n ;

    @Override
       public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.songlist);

        //updates the song list
        updateSongList();
    }

    //update song list called in onCreate()
    public void updateSongList(){
        File home=new File(MEDIA_PATH); 
        songs.clear();
        if(home.listFiles(new Mp3Filter()).length > 0){
            for(File file : home.listFiles(new Mp3Filter())){
                songs.add(file.getName());
                songsarray[i++]=file.getName();

                ArrayAdapter<String> songList = new ArrayAdapter<String>(this,R.layout.song_item,songs);
                setListAdapter(songList);
            }
        }


        registerForContextMenu(getListView());
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
      if (v.getId()==R.id.list) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        menu.setHeaderTitle(songsarray[info.position]);
       // String[] menuItems = getResources().getStringArray(R.array.menu);
        for (int i = 0; i<menuItems.length; i++) {
          menu.add(Menu.NONE, i, i, menuItems[i]);
        }
      }
    }

2 个答案:

答案 0 :(得分:0)

我认为你的条件没有返回true(if(v.getId()== R.id.list))。 所以在onCreateContextMenu()方法中删除这个条件。

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
{    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo; 
        menu.setHeaderTitle(songsarray[info.position]);
        // String[] menuItems = getResources().getStringArray(R.array.menu); 
        for (int i = 0; i<menuItems.length; i++)
             menu.add(Menu.NONE, i, i, menuItems[i]);
} 

尝试使用上面的代码替换onCreateConxtMenu()方法。

答案 1 :(得分:0)

进行这些更改并尝试..

 @Override
public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {
  if (v.getId()==R.id.list) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
    menu.setHeaderTitle(songsarray[info.position]);
   // String[] menuItems = getResources().getStringArray(R.array.menu);
    for (int i = 0; i<menuItems.length; i++) {
      menu.add(Menu.NONE, i, i, menuItems[i]);
    }
  return true;<---(1)
  }
 return false;<---(2)
}