2013-04-04 30 views
-2

通过使用onItemClick和条件我匹配字符串数组与原始文件夹MP3文件我想通过点击列表视图歌曲我需要在其他活动通过调用只是该mplay方法播放歌曲。请帮助我....如果条件在Android

public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
{  
    Intent intent = new Intent(this, Play.class); 
    startActivity(intent); 

    MediaPlayer mPlayer2; 
    MediaPlayer mPlayer3; 




    if(position==0) 
    { 
     public void mplay() **<--------- I Get error in this Line** 
        { 
     mPlayer2= MediaPlayer.create(this, R.raw.gayatri); 
     mPlayer2.start(); 
        } 

    } 
+1

这是一个语法错误。您不能在常规代码块中声明方法。也许学习JAVA语言教程? – 2013-04-04 06:41:34

+0

在另一个Activity中调用一个方法只是:'Play.this.mplay()'并且该方法必须退出该活动 – tttony 2013-04-04 06:43:33

回答

0

你要打电话mplay(),不是创造mpaly()中,如果条件

public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
{  
    Intent intent = new Intent(this, Play.class); 
    startActivity(intent); 

    MediaPlayer mPlayer2; 
    MediaPlayer mPlayer3; 

    if(position==0) 
    { 
     mplay(); 
     //or mplay(R.raw.gayatri); 
    } 


} 

public void mplay() 
        { 
     mPlayer2= MediaPlayer.create(activity.this, R.raw.gayatri); 
     mPlayer2.start(); 
        } 



    public void mplay(int id) 
         { 
      mPlayer2= MediaPlayer.create(activity.this, id); 
      mPlayer2.start(); 
         } 
+0

我可以在一个活动中创建mplay()并在另一个活动中声明mplay方法 – Pramoth 2013-04-04 06:44:19

+0

yes。在任何文件中创建mpalay(),或者在对象的帮助下调用 – 2013-04-04 06:45:36

+0

我需要通过这种方法播放近150个任何特殊方法,通过单击列表视图播放150个歌曲的歌曲 – Pramoth 2013-04-04 08:58:45

0

在Java中,你不能做这样的事情,但你可以返回一个只包含一个方法的匿名类的实例。

0

在项目点击

if(position==0) 
{ 
    mplay(); 
} 

在您的活动定义方法mplay()

 public void mplay() 
     { 

     //do something 
     } 
+0

我需要通过这种方法播放近150个任何特殊的方法,通过点击列表视图从原始文件夹播放150首歌曲 – Pramoth 2013-04-04 08:59:34