[英]Accessing a List from another class
I'm working on an audio player.我正在开发音频播放器。 I have a fragment called HomeFragment which shows the lists of the songs.我有一个名为HomeFragment的片段,它显示了歌曲列表。 When I press one of the songs an activity called SongDetailActivity opens and there is a play button called play_fab inside it.当我按下其中一首歌曲时,会打开一个名为SongDetailActivity的活动,其中有一个名为play_fab的播放按钮。 I want to create a notification when I press this play button.当我按下这个播放按钮时,我想创建一个通知。
I have the song lists inside the DataSource.java .我在DataSource.java 中有歌曲列表。
My problem is in this line我的问题在这一行
SongDetailActivity :歌曲详细活动:
CreateNotification.createNotification(SongDetailActivity.this, lstSongs.get(1),R.drawable.ic_pause_black_24dp,1,lstSongs.size()-1);
lstSongs is colored red, I can't reach it. lstSongs 是红色的,我够不着。 What should I do to reach lstSongs.我应该怎么做才能到达 lstSongs。 Can somebody help please?有人可以帮忙吗?
My codes:我的代码:
SongDetailActivity.java SongDetailActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song_detail);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
play_fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CreateNotification.createNotification(SongDetailActivity.this, lstSongs.get(1),R.drawable.ic_pause_black_24dp,1,lstSongs.size()-1);
}
});
DataSource.java数据源.java
public class DataSource {
public static List<Song> getMusic(){
List<Song> lstSongs = new ArrayList<>();
lstSongs.add(new Song("Title1","Artist1", R.drawable.cover1,"Category Name 1"));
lstSongs.add(new Song("Title2","Artist2",R.drawable.cover2,"Category Name 2"));
lstSongs.add(new Song("Title3","Artist3",R.drawable.cover3,"Category Name 3"));
lstSongs.add(new Song("Title4","Artist4",R.drawable.cover4,"Category Name 4"));
lstSongs.add(new Song("Title5","Artist5",R.drawable.cover5,"Category Name 5"));
lstSongs.add(new Song("Title6","Artist6",R.drawable.cover6,"Category Name 6"));
lstSongs.add(new Song("Title7","Artist7",R.drawable.cover7,"Category Name 7"));
return lstSongs;
}
Create a global variable List<Song> lstSongs;
创建一个全局变量List<Song> lstSongs;
and instantiate in onCreate()
of SongDetailActivity
as follows:并在SongDetailActivity
onCreate()
中实例化如下:
lstSongs = DataSource.getMusic();
It should serve your purpose.它应该为您的目的服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.