将arraylist的每个元素显示到setText

时间:2012-08-29 17:01:29

标签: android list android-intent arraylist

我想将arraylist的每个元素或值显示到tv.setText。

arraylist的值来自1stactivity(第一个屏幕),我想将它传递给第二个活动的tv.setText(第二个屏幕)。

这是第一项活动的代码

List<String> class_code = new ArrayList<String>();
class_code.add("test");
class_code.add("test2");
Intent intent = new Intent(1stscreen.this,2nd_screen.class);
intent.putStringArrayListExtra("code", (ArrayList<String>) class_code);

以下是第二项活动的代码

tv.setText(getIntent().getExtras().getString("code"));

但它显示了arraylist的所有价值(test和test2)我只想获得arraylist的第一个值。

5 个答案:

答案 0 :(得分:2)

要在TextView中仅显示ArrayList中的一个项目,您需要仅传递 第一个项目:

List<String> list = getIntent().getExtras().getStringArrayListExtra("code");
tv.setText(list.get(0));

如果您只计划在下一个Activity中使用此一个字符串,则不传递整个ArrayList,只传递您要使用的String:

Intent intent = new Intent(1stscreen.this, 2nd_screen.class);
intent.putString("code", class_code.get(0));

答案 1 :(得分:0)

如果你只想要一个值使用类似这样的class_code.get(0)

intent.putString("code", class_code.get(0));

您可以使用

从下一个活动中获取它
getintent().getExtras().getString("code");

答案 2 :(得分:0)

如果您只想使用第一个值,则只传递第一个值。而不是

intent.putStringArrayListExtra("code", (ArrayList<String>) class_code);

尝试放

intent.putStringExtra("code", class_code.get(0));

答案 3 :(得分:0)

使用此:

List<String> list = getIntent().getStringArrayListExtra("code");
tv.setText(list.get(0));

答案 4 :(得分:0)

我需要将值从第一个屏幕转发到第三个屏幕,所以我使用

解决了这个问题

第一个屏幕

intent.putStringArrayListExtra("class_code", (ArrayList<String>) class_code);

第二屏

intent.putStringArrayListExtra("class_code", getIntent().getExtras().getStringArrayList("class_code"));

第3个屏幕

tv.setText(getIntent().getExtras().getStringArrayList("class_code").get(0));