Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何发送ArrayList<;对象>;从一个活动到第二个活动?_Android_Android Activity - Fatal编程技术网

Android 如何发送ArrayList<;对象>;从一个活动到第二个活动?

Android 如何发送ArrayList<;对象>;从一个活动到第二个活动?,android,android-activity,Android,Android Activity,我想将“ArrayList objArrayList”从一个活动传递到第二个活动,并希望在那里接收。我正在创建一个简单的类,其中有四个arraylist。我正在创建该类的对象,并通过传递要添加到arraylist中的参数来调用该类的方法。之后,我将在objArrayList中添加该类对象。 如何将objArrayList从一个活动传递到第二个活动并在那里接收 谢谢, 维沙哈 使用标准的意图机制。将您的列表放入额外的意图中并传递给他人。第一个上下文(可以是活动/服务等) 您有几个选择: 1) 从以

我想将“ArrayList objArrayList”从一个活动传递到第二个活动,并希望在那里接收。我正在创建一个简单的类,其中有四个arraylist。我正在创建该类的对象,并通过传递要添加到arraylist中的参数来调用该类的方法。之后,我将在objArrayList中添加该类对象。 如何将objArrayList从一个活动传递到第二个活动并在那里接收

谢谢,
维沙哈

使用标准的意图机制。将您的列表放入额外的意图中并传递给他人。

第一个上下文(可以是活动/服务等)

您有几个选择:

1) 从以下位置使用:

2) 创建一个新包

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);
3) 使用意图的快捷方式方法

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);
新上下文(可以是活动/服务等)

注意:对于所有基本类型、包和序列化包,包都有“get”和“put”方法。我只是出于演示的目的使用字符串

Arraylist类必须是可打包或可序列化的。因此,您应该对它进行子类化,以添加这些功能,因为我认为它们没有从原始实现中获得

然后看看这些问题,它们会帮助你。

对于选项2,我看不到任何适用于mBundle的“附加”。
Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);
Intent myIntent = getIntent(); // this getter is just for example purpose, can differ
if (myIntent !=null && myIntent.getExtras()!=null)
     String value = myIntent.getExtras().getString(key);
}