更好地传递Android的活动之间的数据的方法 [英] Better way to pass data between Activities in Android

查看:117
本文介绍了更好地传递Android的活动之间的数据的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我从JavaScript提取数据,因为它是不可能将数据作为数组或对象,我返回一个字符串返回。

In my application I am fetching data from JavaScript, as it is not possible to return the data as an array or object, I am returning it as a String.

现在来组织数据我创建包含的ArrayList 和其他字符串变量,并进一步我创造我的类对象的数组变量来存储多个记录的类。

Now to organize the data I am creating a class which contains ArrayLists and other string variables and further I am creating array of my class objects variable to store multiple records.

public class Data {

    ArrayList<String> m_empArrayList = new ArrayList();
    ArrayList<String> m_depArrayList = new ArrayList();
    String m_time;
    String m_duration;

}

Data d = new Data();

这将是一个很好的方法来传递活动之间的数据?由于意图撕碎prefrence s的用于传递数据的小户型,我不考虑在这里

What would be a good approach to pass the data between Activities? As Intents and ShredPrefrences are used to pass small units of data I am not considering it here.

推荐答案

实施 Parcelable 界面自定义对象,并通过发送它意图

Implement the Parcelable interface in your custom object and transmit it via an Intent.

下面是一个 Parcelable 对象的例子。

Here is an example of a Parcelable object.

public class MyObject implements Parcelable {

    private String someString = null;
    private int someInteger = 0;

    public MyObject() {
        // perform initialization if necessary
    }

    private MyObject(Parcel in) {
        someString = in.readString();
        someInteger = in.readInt();
    }

    public static final Parcelable.Creator<MyObject> CREATOR =
            new Parcelable.Creator<MyObject>() {

        @Override
        public MyObject createFromParcel(Parcel source) {
            return new MyObject(source);
        }

        @Override
        public MyObject[] newArray(int size) {
            return new MyObject[size];
        }

    };

    // Getters and setters

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(someString);
        dest.writeInt(someInteger);
    }

}

下面是发生了什么。如果实施 Parcelable 接口,你必须创建一个私有的构造,这需要的 包裹 作为参数。这包裹保存所有的序列化值。

Here is what happens. If you implement the Parcelable interface you have to create a private constructor which takes a Parcel as a parameter. That Parcel holds all the serialized values.

您必须实现嵌套类 Parcelable.Creator 名为 CREATOR ,因为这将是由被称为重新创建对象时,Android系统。

You must implement the nested class Parcelable.Creator with the name CREATOR as this is going to be called by Android when recreating your object.

该方法<一href=\"http://developer.android.com/reference/android/os/Parcelable.html#describeContents%28%29\"><$c$c>describeContents()只有在特殊情况下使用。你可以离开,因为它是一个返回值 0

有趣的动作发生在<一href=\"http://developer.android.com/reference/android/os/Parcelable.html#writeToParcel%28android.os.Parcel,%20int%29\"><$c$c>writeToParcel()在那里你,正如其名字告知,你的数据写入包裹对象。

The interesting action happens in writeToParcel() where you, as the name tells, write your data to a Parcel object.

现在你可以直接添加自定义对象的意图就像这个例子。

Now you can just add your custom object directly to an Intent like in this example.

MyObject myObject = new MyObject();
Intent i = new Intent();
i.setExtra("MY_OBJECT", myObject);
// implicit or explicit destination declaration
startActivity(i);

这篇关于更好地传递Android的活动之间的数据的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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