码迷,mamicode.com
首页 > 移动开发 > 详细

android activity传递实体类对象

时间:2015-11-26 12:54:22      阅读:446      评论:0      收藏:0      [点我收藏+]

标签:

通过实现Parcelable接口序列化对象的步骤:

1、实现Parcelable接口。
2、并且实现Parcelable接口的public
 void writeToParcel(Parcel dest, int flags)方法 。
3、自定义类型中必须含有一个名称为CREATOR的静态成员,该成员对象要求实现Parcelable.Creator接口及其方法。

 

public class Person implements Parcelable {
 
    public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
 
        @Override
        public Person[] newArray(int size) {
            return null;
        }
 
        @Override
        public Person createFromParcel(Parcel source) {
            Person result = new Person();
            result.age = source.readInt();
            result.name = source.readString();
            result.address = source.readParcelable(Address.class.getClassLoader());
            return result;
        }
    };
    private Address address;
    private String name;
    private int age;
 
    @Override
    public int describeContents() {
        return 0;
    }
 
    public Address getAddress() {
        return address;
    }
 
    public void setAddress(Address address) {
        this.address = address;
    }
 
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(age);
        dest.writeString(name);
        dest.writeParcelable(address, PARCELABLE_WRITE_RETURN_VALUE);
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    @Override
    public String toString() {
        return "Person [address=" + address + ", name=" + name + ", age=" + age
                + "]";
    }
 
}

 

 

 

传递实体类:

Bundle bundle = new Bundle();
// bundle.putSerializable(TITLE, title);
bundle.putParcelable(TITLE, title);
context.startActivity(new Intent(context, target).putExtras(bundle));

接收:

mCollectParamObj = (CollectParamObj) getIntent().getParcelableExtra(
"title");

 

android activity传递实体类对象

标签:

原文地址:http://www.cnblogs.com/zhaoleigege/p/4997090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!