2014-09-27 212 views
-1

这可能是一个愚蠢的问题。Android音乐播放器:如何显示歌曲标题

我有一个ListView的歌曲,当点击ListView中的一个项目时,我打开另一个意图来显示专辑封面。

的ListView:http://i.imgur.com/24s7Ema.png 球员:http://i.imgur.com/Rq5Ce7d.png

在第二意图“球员”我如何显示当前播放的歌曲名称名称存在。

SONG.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:onClick="songPicked" 
android:padding="5dp" > 

<TextView 
    android:id="@+id/song_title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#0000FF" 
    android:textSize="20sp" 
    android:textStyle="bold" /> 

<TextView 
    android:id="@+id/song_artist" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#FF0000" 
    android:textSize="18sp" /> 

</LinearLayout> 

SONGADAPTER.JAVA

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    //Map to song layout 
    LinearLayout songLay = (LinearLayout)songInf.inflate(R.layout.song, parent, false); 

    //get title and artist views 
    TextView songView = (TextView) songLay.findViewById(R.id.song_title); 
    TextView artistView = (TextView) songLay.findViewById(R.id.song_artist); 

    //get the correct Song instance using current position index 
    Song currSong = songs.get(position); 

    //get title and artist strings (Mapping the strings to views in song Layout file) 
    songView.setText(currSong.getTitle()); 
    artistView.setText(currSong.getArtist()); 

    //set the position as tag (play the correct song when clicking an item in the list) 
    songLay.setTag(position); 

    /*song.xml has an onClick attribute, the method is used to retrieve the tag in the activity*/ 
    return songLay; 
} 

MUSICPLAYER.JAVA(FIRST LISTVIEW)

public void songPicked(View view) 
{ 
    /*Song position (item in list view) has been set as Tag in SongAdapter*/ 
    musicSrv.setSong(Integer.parseInt(view.getTag().toString())); //retrieve position and pass to Service instance 

    /*Starting the NowPlaying activity*/ 
    Intent in = new Intent(getApplicationContext(), NowPlaying.class); 
    setResult(100,in); 
    startActivity(in); 
    musicSrv.playSong(); 

}

NOWPLAYING.JAVA(NEW意图播放器控制)

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    if(resultCode==100) 
    { 
     //Code here 
    } 
} 
+1

显示您的代码。因为它太愚蠢的问题。 – Piyush 2014-09-27 04:48:30

+0

这是我的项目:http://www.filedropper.com/musicplayer。这有帮助吗?是尴尬,这是一个愚蠢的问题。但是谢谢你试图帮助我。 – jackuars 2014-09-27 05:07:09

+0

伴侣,我不需要整个项目我只想要你的'意图'的相关代码。 – Piyush 2014-09-27 05:09:54

回答

0

您需要实施OnItemClickListener方法ListView

listView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // TODO Auto-generated method stub 
      Intent n = new Intent(YourActyivityName.this, YourNewctivity.class); 
      n.putExtra("song_name", urArrayListName.get(position).get("songName"));// songName is key which you stoared in arraylist. here i just write as a sample. 
      startActivity(n); 
     } 
    }); 

现在,在您新的活动让您的数据,

Intent n = getIntent(): 
String songName = n.getStringExtra("song_name"); 

现在,在您TextView设置此字符串。

txtView.setText(songName): 
+0

谢谢,但我有一个自定义的ListView,并且onClick实例不是以ListView开始,而是通过LinearLayout显示songTitle和songArtist的扩展布局SONG.XML。 SONG.XML有一个onClick属性“songPicked”,该方法用于检索活动中的标签,并启动新的Intent以及歌曲Plyback。我在SongAdapter.java中将该位置设置为标签。我已经在第一篇文章中更新了代码。 – jackuars 2014-09-27 05:53:33