【发布时间】:2011-05-24 15:54:51
【问题描述】:
我在 First Activity - A 中有 2 个字符串数组,现在我需要将两个数组都传递给 second_activity - B,我该怎么做?
我知道 Android 中的 Intent 类概念,并且已经将单个变量值传递给另一个活动,但我还没有实现在活动之间传递字符串数组的概念,我有已经上网了。
请告诉我可能的解决方案。
【问题讨论】:
标签: android
我在 First Activity - A 中有 2 个字符串数组,现在我需要将两个数组都传递给 second_activity - B,我该怎么做?
我知道 Android 中的 Intent 类概念,并且已经将单个变量值传递给另一个活动,但我还没有实现在活动之间传递字符串数组的概念,我有已经上网了。
请告诉我可能的解决方案。
【问题讨论】:
标签: android
不是直接回答问题,但您也可以在捆绑包中使用 .putStringArrayListExtra()。比发送字符串数组更灵活。
Bundle b=new Bundle();
b.putStringArrayListExtra("URL_ARRAY_LIST",
myStringArrayList);
Intent i=new Intent(context, Class);
i.putExtras(b);
那么就可以得到这个arrayList如下:
ArrayList<String> urls;
urls = getIntent().getStringArrayListExtra("URL_ARRAY_LIST");
【讨论】:
Intent 将数据携带到键值映射中,其中“key”是您在将数据存储到 Intent 时选择的字符串名称标识符。读取该数据时,您请求相同的“密钥”。您可以在单个 Intent 中存储各种数据类型。
【讨论】:
Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);
希望这会对您有所帮助。
为了阅读:
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
【讨论】: