[英]How to get data from onitemclick and display it on another activity (Android Dev)?
I am very new to Android and Java dev, sorry in advance.我对 Android 和 Java 开发人员非常陌生,提前抱歉。
I am making a simple music player app.我正在制作一个简单的音乐播放器应用程序。
Right now my app gets storage data from the phone (it finds mp3 files) and displays both the title and artist in a ListView.现在我的应用程序从手机获取存储数据(它找到 mp3 文件)并在 ListView 中显示标题和艺术家。
When a song item in the list is clicked it changes screens to the PlaySongActivity.Java via an intent.当单击列表中的歌曲项时,它会通过意图将屏幕更改为 PlaySongActivity.Java。 On the Activity_Play_Song.xml I set up two text views.
在 Activity_Play_Song.xml 我设置了两个文本视图。 I want to make it so that whenever an item is clicked the title and artist is transferred into the two text views in the Activity_Play_Song.xml texts.
我想这样做,以便每当单击一个项目时,标题和艺术家都会转移到 Activity_Play_Song.xml 文本中的两个文本视图中。
How can I do this?我怎样才能做到这一点?
Here is my code:这是我的代码:
MainActivity主要活动
ArrayList<String> arrayList;
ListView listView;
ArrayAdapter<String> adapter;
public void getMusic(){
ContentResolver contentResolver = getContentResolver();
Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor songCursor = contentResolver.query(songUri, null, null, null, null);
if(songCursor != null && songCursor.moveToFirst()){
int songTitle = songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int songArtist = songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
do{
String currentTitle = songCursor.getString((songTitle));
String currentArtist = songCursor.getString((songArtist));
arrayList.add(currentTitle+ "\n" + currentArtist);
} while (songCursor.moveToNext());
}
}
public void doStuff(){
listView = (ListView) findViewById(R.id.listView);
arrayList = new ArrayList<>();
getMusic();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(MainActivity.this, PlaySongActivity.class);
startActivity(i);
}
});
}
Currently, the PlaySongsActivity only contains the default onCreate code and the activity_play_songs.xml has two TextViews where I want the title and artist to go.目前,PlaySongsActivity 仅包含默认的 onCreate 代码,并且 activity_play_songs.xml 有两个 TextView,我希望将标题和艺术家设置为 go。
Anjali bhardwaj.安贾利·巴德瓦杰。
To send information from one activity to another using intent do this:要使用意图将信息从一个活动发送到另一个活动,请执行以下操作:
Intent i = new Intent(MainActivity.this, PlaySongActivity.class);
//Here we store the title and author strings using .putExtra("Name", Value)
i.putExtra("ArtistString", currentArtist);
i.putExtra("TitleString", currentTitle);
startActivity(i);
Now it is stored and will be sent over to the activity that you are starting.现在它被存储并将被发送到您正在启动的活动。
To get the information in the receiving activity在接收活动中获取信息
/*gets the information from the intent that was passed and casts it to a Intent object*/
Intent intent = getIntent();
//gets the strings from the intent
String artist = intent.getStringExtra("ArtistString");
String title = intent.getStringExtra("TitleString");
Now you can do whatever you want with this string.现在你可以用这个字符串做任何你想做的事情。 Hope this helps!
希望这可以帮助!
--UPDATE-- - 更新 -
Example例子
public class SomeClass{
//declare them outside of the method so that they can be accessed by any method.
//I just set them to nothing so that you dont get NULLPOINTEREXCEPTIONs
String currentTitle = "nothing";
String currentArtist = "nothing";
public void getMusic(){
//set the value of the strings in here
}
public void doStuff(){
//Access them here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.