android - 如何将 ArrayList<object> 从一个 Activity 发送到第二个 Activity ?

标签 android android-activity

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

谢谢, 维沙卡。

最佳答案

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

您有几个选择:

1) 使用 Bundle来自 Intent :

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);  

2) 创建一个新的包

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);

3) 使用 putExtra() Intent的快捷方式

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);

新上下文(可以是 Activity/服务等)

Intent myIntent = getIntent(); // this getter is just for example purpose, can differ
if (myIntent !=null && myIntent.getExtras()!=null)
     String value = myIntent.getExtras().getString(key);
}

注意:Bundle 具有适用于所有基本类型、Parceable 和 Serializable 的“get”和“put”方法。我只是将字符串用于演示目的。

您的 Arraylist 类必须是 Parcelable 或 Serializable。因此,您应该将其子类化以添加这些功能,因为我认为它们在原始实现中遗漏了。

然后研究这些其他问题,它们会对您有所帮助。 Serialization issue with SortedSet, Arrays, an Serializable

关于android - 如何将 ArrayList<object> 从一个 Activity 发送到第二个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5322250/

相关文章:

android - doOnSubscribe 在主线程上被调用

初始化数组对象时出现java.lang.ArrayIndexOutOfBoundsException

android - 不是从 Intent 开始 Activity

android - 为什么这些 Android 控件在 Galaxy S 上看起来很奇怪?

Android检查应用程序是否关闭

java - 我如何从 Activity 中更改 fragment 的 TextView 文本

android - 如何检查 SharedPreferences 是否存在

android - Firebase 云消息传递 API (V1) : Server Key missing

Android 回调 - 这是潜在的内存泄漏吗?

java - 来电期间 Activity 生命周期的异常行为 : delayed onStop()