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