Android学习——Intent传递复杂数据类型

来源:互联网 发布:windows安装服务 编辑:程序博客网 时间:2024/06/05 15:02

目标:单击Button实现从MainActivity跳转到OtherActivity,并携带一个Person类对象,在OtherActiviy中打印出来。
具体实现如下:
Dog类,实现了Serializable接口:

public class Dog implements Serializable{    private String name;    private int age;    public Dog(String name, int age) {        this.name = name;        this.age = age;    }    public Dog() {    }    @Override    public String toString() {        return "Dog{" +                "name='" + name + '\'' +                ", age=" + age +                '}';    }}

Person类,实现了Parcelable接口:

public class Person implements Parcelable{    private String name;    private Dog dog;    private List<String> hobbys;    private List<Dog> dogs;    public Person() {    }    public Person(String name) {        this.name = name;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Dog getDog() {        return dog;    }    public void setDog(Dog dog) {        this.dog = dog;    }    public List<String> getHobbys() {        return hobbys;    }    public void setStrs(List<String> hobbys) {        this.hobbys = hobbys;    }    public List<Dog> getDogs() {        return dogs;    }    public void setDogs(List<Dog> dogs) {        this.dogs = dogs;    }    @Override    public String toString() {        return "Person{" +                "name='" + name + '\'' +                ", dog=" + dog +                ", hobbys=" + hobbys +                ", dogs=" + dogs +                '}';    }    public static final Creator<Person> CREATOR = new Creator<Person>() {        @Override        public Person createFromParcel(Parcel parcel) {            Person person = new Person();            //从parcel中读取数据封装成Person对象返回            //注意:读的顺序一定要与写的顺序一致            person.name = parcel.readString();            person.dog = (Dog) parcel.readSerializable();            List<String> list = new ArrayList<>();            parcel.readStringList(list);            person.hobbys = list;            person.dogs = parcel.readArrayList(this.getClass().getClassLoader());            return person;        }        @Override        public Person[] newArray(int i) {            return new Person[0];        }    };    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel parcel, int i) {        //写数据:记住顺序        parcel.writeString(name);        parcel.writeSerializable(dog);        parcel.writeStringList(hobbys);        parcel.writeList(dogs);    }}

MainActivity中按钮监听事件:

//实例化Person对象Person person = new Person("韩梅梅");//设置String集合List<String> hobbys = new ArrayList<>();hobbys.add("吃饭");hobbys.add("睡觉");hobbys.add("打豆豆");person.setStrs(hobbys);//设置对象Dog xiaohei = new Dog("小黑",3);person.setDog(xiaohei);//设置对象集合Dog xiaohuang = new Dog("小黄",2);Dog dahuang = new Dog("大黄",5);List<Dog> cats = new ArrayList<Dog>();cats.add(xiaohuang);cats.add(dahuang);person.setDogs(cats);Intent intent = new Intent(this,OtherActivity.class);//传递person对象intent.putExtra("per",person);this.startActivity(intent);

OtherActivity从Intent中取出数据打印:

//拿出数据,并打印Intent intent= this.getIntent();Person person = intent.getParcelableExtra("per");System.out.println("name = " + person.getName());System.out.println("dog = " + person.getDog());System.out.println("hobbys = " + person.getHobbys());System.out.println("dogs = " + person.getDogs());

从控制台查看输出结果:
这里写图片描述

0 0
原创粉丝点击