2012-08-29 41 views
0

我想将arraylist的每个元素或值显示为tv.setText。显示arraylist的每个元素以设置文本

arraylist的值来自1stactivity(第1屏幕),我想将它传递给第2个activity(第2屏幕)的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(测试和测试2)的所有价值,我只想要得到的第一数组列表的值。

回答

2

从一个ArrayList在TextView中显示只有一个项目,你需要通过第一项:

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

如果你只打算在接下来的活动不要使用此一个字符串“T传递整个ArrayList中,只有把这个字符串要使用:

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

如果你想只有一个值使用像这样class_code.get(0)

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

,您可以从下一个活动使用

getintent().getExtras().getString("code"); 
0

得到它,如果你只想使用的第一个值,那么只有通过第一个值。取而代之的

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

尝试把

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

使用此:

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

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

第一屏

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

第2屏幕

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

3屏幕

tv.setText(getIntent().getExtras().getStringArrayList("class_code").get(0)); 
相关问题