Android音乐播放器:如何显示歌曲标题

时间:2022-12-14 21:25:54

this is probably a silly question.

这可能是一个愚蠢的问题。

I've a ListView of songs and when clicking an item in the ListView, I open another intent to display the album art.

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

ListView: http://i.imgur.com/24s7Ema.png Player: http://i.imgur.com/Rq5Ce7d.png

ListView:http://i.imgur.com/24s7Ema.png播放器:http://i.imgur.com/Rq5Ce7d.png

In the second intent 'Player' how to I display the currently playing Song Title name there.

在第二个意图'播放器'中如何在那里显示当前正在播放的歌曲名称。

SONG.XML

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

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)

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 INTENT WITH PLAYER CONTROLS)

NOWPLAYING.JAVA(与球员控制的新目标)

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

1 个解决方案

#1


0  

You need to implement OnItemClickListener method for ListView.

您需要为ListView实现OnItemClickListener方法。

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);
        }
    });

Now in your new Activity get your data,

现在在您的新活动中获取您的数据,

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

Now set this String in your TextView.

现在在TextView中设置此String。

txtView.setText(songName):

#1


0  

You need to implement OnItemClickListener method for ListView.

您需要为ListView实现OnItemClickListener方法。

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);
        }
    });

Now in your new Activity get your data,

现在在您的新活动中获取您的数据,

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

Now set this String in your TextView.

现在在TextView中设置此String。

txtView.setText(songName):