通过对象从一个活动到另一个最好的方法 [英] Best way to pass objects from one activity to another

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

问题描述

我看了一下全局静态方法中,我们创建一个类的静态字段可以在任何活动进行访问。是否有通过大型数据集如ArrayList或HashMaps这样任何其他方式? 我也看到有关Serializable接口,但不知道如何使用它。任何例如code是欢迎的......

解决方案

 意向意图=新的意图(getBaseContext(),NextActivity.class);
intent.putExtra(数组列表,新的ArrayList<字符串>());
 

如果您ArrayList中包含您自己创建,例如Friend.class另一个对象,你可以实现Friend.class与序列化,然后:

 意向意图=新的意图(getBaseContext(),NextActivity.class);
intent.putExtra(好友列表,新的ArrayList&其中;朋友>());
 

和接受它NextActivity.class:

 捆绑额外= getIntent()getExtras()。
    如果(临时演员!= NULL){
        ArrayList的<朋友>朋友= extras.getSerializable(好友列表);
    }
 

好,而不是传递一个空的ArrayList,你必须把值到ArrayList中,然后通过它,但你的想法。

I have read about global static technique in which we create a class with static fields which can be accessed in any activity. Is there any other way to pass large data sets like ArrayList or HashMaps ? I have also read about Serializable but have no idea how to use it. Any example code is welcome...

解决方案

Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("arraylist", new ArrayList<String>());

If your ArrayList contains another Object that you have created yourself, for instance Friend.class, you can implement the Friend.class with Serializable and then:

Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("friendlist", new ArrayList<Friend>());

And for receiving it on NextActivity.class:

Bundle extras = getIntent().getExtras();
    if(extras != null){
        ArrayList<Friend> friends = extras.getSerializable("friendlist");
    }

Well, instead of passing an empty ArrayList, you'll have to put values into the ArrayList and then pass it, but you get the idea.

这篇关于通过对象从一个活动到另一个最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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