如何传递 ArrayList<CustomeObject>从一项活动到另一项活动? [英] How to pass ArrayList&lt;CustomeObject&gt; from one activity to another?

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

问题描述

我想将以下 ArrayList 从一个活动发送到另一个活动,请帮忙.

I want to send Following ArrayList from one activity to another please help.

ContactBean m_objUserDetails = new ContactBean();
ArrayList<ContactBean> ContactLis = new ArrayList<ContactBean>(); 

我在添加数据后发送上面的arraylist,如下

I am sending the above arraylist after adding data in it as follows

  Intent i = new Intent(this,DisplayContact.class);
  i.putExtra("Contact_list", ContactLis);
  startActivity(i);

但是我在恢复它时遇到了问题.

But I am getting problem while recovering it.

ArrayList<ContactBean> l1 = new ArrayList<ContactBean>();
Bundle wrapedReceivedList = getIntent().getExtras();
l1= wrapedReceivedList.getCharSequenceArrayList("Contact_list");

此时我收到此错误:

Type mismatch: cannot convert from ArrayList<CharSequence> to ArrayList<ContactBean>

我的 ContactBean 类实现了 Serializable 请告诉我们为什么要实现可序列化的接口.

My ContactBean class implements Serializable please also tell why we have to implement serializable interface.

推荐答案

如果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");

这篇关于如何传递 ArrayList<CustomeObject>从一项活动到另一项活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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