简体   繁体   English

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

[英]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.

相关问题 如何使用 ArrayList HashMap 将 listview onItemClick 数据发送到另一个活动 - How to send listview onItemClick data to another activity with ArrayList HashMap 如何从Android GridView onItemClick添加新活动? - How to add New Activity from Android GridView onItemClick? 显示详细信息 firebase 数据来自 listview onitemclick Android Studio - Display Detail firebase data from listview onitemclick Android Studio 无法将数据从一个活动获取到另一个活动 Android - Unable to get data from one activity to another activity Android 如何将数据从活动传递到Android上的另一个活动 - How to pass data from activity to another activity on Android Android如何在Arraylist中显示图像来自另一个活动的意图? - Android how to display image in Arraylist which intent from another activity? (onItemClick)从列表视图显示Json数据-Android - (onItemClick) show Json data from listview - Android 如何从Firebase推送通知中获取数据并将其显示在Android活动中? - How to get the data from Firebase Push Notification and display it in your Android Activity? 如何显示一个活动到另一个活动的数据并同时保存它? - How to display data from an activity to another and in the same time to save it? Android:从列表中获取项目(使用onItemClick)并将该项目数据传递到另一个布局? - Android : getting an item from a list (using onItemClick) and pass that Item data to another layout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM