从一个活动转移目标到另一个活动

从一个活动转移目标到另一个活动

本文介绍了从一个活动转移目标到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类EmployeeInfo如下:

I am having a class EmployeeInfo as the following :

 public class EmployeeInfo {
        private int id; // Employee ID
        private String name; // Employee Name
        private int age;// Employee Age

        public int getEmployeeID() {
            return id;
        }

        public void setEmployeeID(int id) {
            this.id = id;
        }

        public String getEmployeeName() {
            return name;
        }

        public void setEmployeeName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age= age;
        }
    }


ArrayList<EmployeeInfo> employeeInfo object contains the emplyoyee info data for multiple employees.

我想从活动1中的数据(ArrayList中employeeInfo)转移到活性2。

I want to transfer the data( ArrayList employeeInfo ) from Activity1 to Activity2.

时使用Parcelable的数据传输活动1至活性2的唯一途径?如果不是,有什么办法。

Is using Parcelable the only way to transfer the data from Activity1 to Activity2?If not , what are the alternatives.

如果是,请连同有关如何将目标数据传输活动1至活性2样品code提供Parcelable的原型code。

If yes ,kindly provide the prototype code of Parcelable along with the sample code on how to transfer the object data from Activity1 to Activity2.

在此先感谢。

亲切问候,

CB

推荐答案

下面是我实现Parceleble的:

Here is my implementation of Parceleble:

public class ProfileData implements Parcelable {

private int gender;
private String name;
private String birthDate;

public ProfileData(Parcel source) {
    gender = source.readInt();
    name = source.readString();
    birthDate = source.readString();
}

public ProfileData(int dataGender, String dataName, String dataBDate) {
    gender = dataGender;
    name = dataName;
    birthDate = dataBDate;
}

// Getters and Setters are here

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

@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(gender);
out.writeString(name);
out.writeString(birthDate);
}

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

public ProfileData createFromParcel(Parcel in) {
    return new ProfileData(in);
}

public ProfileData[] newArray(int size) {
    return new ProfileData[size];
}

};

}

和我是如何传输的数据:

and how I transfer data:

Intent parcelIntent = new Intent().setClass(ActivityA.this, ActivityB.class);
ProfileData data = new ProfileData(profile.gender, profile.getFullName(), profile.birthDate);
parcelIntent.putExtra("profile_details", data);
startActivity(parcelIntent);

和获取数据:

    Bundle data = getIntent().getExtras();
    ProfileData profile = data.getParcelable("profile_details");

这篇关于从一个活动转移目标到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:58