将列表从一个活动传递到另一个活动 [英] Passing a List from one Activity to another

查看:40
本文介绍了将列表从一个活动传递到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将ArrayList 等集合从一个Activity 传递到另一个,就像我们过去传递Stringsint在Intent的putExtra方法的帮助下?

How to pass Collections like ArrayList, etc from one Activity to another as we used to pass Strings, int with help of putExtra method of Intent?

任何人都可以帮助我,因为我想将 List 从一个 Activity 传递给另一个?

Can anyone please help me as i want to pass a List<String> from one Activity to another?

推荐答案

如果E类型是ArrayList>可序列化.

You can pass an ArrayList<E> the same way, if the E type is Serializable.

调用IntentputExtra(String name, Serializable value)进行存储,调用getSerializableExtra(String name)进行检索.

You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval.

示例:

ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);

在另一个活动中:

ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");

<小时>

请注意,序列化会导致性能问题:需要时间,并且会分配大量对象(因此必须进行垃圾回收).

这篇关于将列表从一个活动传递到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆