繁体   English   中英

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

[英]How to get data from onitemclick and display it on another activity (Android Dev)?

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

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

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

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

我怎样才能做到这一点?

这是我的代码:

主要活动

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,我希望将标题和艺术家设置为 go。

安贾利·巴德瓦杰。

要使用意图将信息从一个活动发送到另一个活动,请执行以下操作:

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

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

在接收活动中获取信息

/*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
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM