gpt4 book ai didi

java - 如何从 onitemclick 获取数据并将其显示在另一个 Activity (Android Dev)上?

转载 作者:行者123 更新时间:2023-12-01 16:21:51 24 4
gpt4 key购买 nike

我对 Android 和 Java 开发非常陌生,提前抱歉。

我正在制作一个简单的音乐播放器应用程序。

现在,我的应用程序从手机获取存储数据(它查找 mp3 文件)并在 ListView 中显示标题和艺术家。

当单击列表中的歌曲项目时,它会通过 Intent 将屏幕更改为 PlaySongActivity.Java。在 Activity_Play_Song.xml 上,我设置了两个 TextView 。我想要做到这一点,以便每当单击某个项目时,标题和艺术家都会转移到 Activity_Play_Song.xml 文本中的两个 TextView 中。

我该怎么做?

这是我的代码:

主要 Activity

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

}
});


}

目前,PlaySongsActivity 仅包含默认的 onCreate 代码,而 Activity_play_songs.xml 有两个 TextView,我希望标题和艺术家位于其中。

最佳答案

安贾利·巴德瓦吉。

要使用 Intent 将信息从一个 Activity 发送到另一个 Activity ,请执行以下操作:

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

现在它已存储并将发送到您正在启动的 Activity 。

获取接收 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");

现在你可以用这个字符串做任何你想做的事情。希望这有帮助!

--更新--

示例

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
}

}

关于java - 如何从 onitemclick 获取数据并将其显示在另一个 Activity (Android Dev)上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62251213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com