简体   繁体   English

如何将ListView项目的文本传递给另一个活动?

[英]How to pass text of ListView item to another activity?

I made a String array which I used to populate a listview in main activity. 我制作了一个String数组,用于在主要活动中填充列表视图。 Now I want to pass the text of the list item to another activity when I click on any list item. 现在,我想在单击任何列表项时将列表项的文本传递给另一个活动。 For example if I have an array: 例如,如果我有一个数组:

String[] names = {"alfred","james","chris","jason"};

A list is populated by these names as: 这些名称填充的列表为:

alfred
james
chris
jason

and if I click on say james, then this should be passed to another activity and be printed in a textview. 如果我单击“说詹姆斯”,则应将其传递给另一个活动并在textview中打印。 Please also tell me how to receive that value on the second activity. 还请告诉我如何在第二项活动中获得该价值。

I use ArrayList instead, is more optimized 我改用ArrayList进行了优化

ArrayList<String> list = new ArrayList<>();
list.add("yourstring");
list.add("yourstring");
list.add("yourstring");


yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent it = new Intent(this, Activity.class);
                it.putExtra("YourKeyHere", list.get(position));
                startActivity(intent);
            }
        });
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(this, YourActivity.class);
                intent.putExtra("TEXT", names[position]);
                startActivity(intent);
            }
        });

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

相关问题 如何在下一个活动中将选定的列表视图项传递给另一个列表视图 - How to pass selected listview item to another listview in the next activity 如何将Listview选定的项目值传递给另一个活动 - How to pass Listview selected item values to another activity 如何使用contextMenu将值传递给listview中项目的另一个活动 - How to pass the value to another activity of an item from listview using contextMenu 如何将列表视图中选定项目的数据传递给另一个活动? - How to pass data from a selected item in a listview to another activity? 将名称从列表视图项传递到另一个活动 - Pass the name from listview item to another activity 将listview选定的文本传递给另一个活动 - Pass listview selected text to another activity 如何在Android中将Listview项目数据(图像和文本)从一个活动发送到另一个活动 - How to send Listview item data (image & text) from one activity to another activity in android android如何将listview项目发送到另一个活动 - android How to send listview item to another activity 如何从一个活动的列表视图传递数据并将其传递给另一活动? - How to pass the data from listview of one activity and pass it to another activity? 如何将从服务器获取的listview项的ID传递给另一个活动onclick? - How do I pass id of listview item I got from server to another activity onclick?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM