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

这篇博客详细介绍了如何在Android中通过Intent传递复杂数据类型,如自定义的Person类对象。通过让Person类实现Parcelable接口,博主展示了在MainActivity中创建Person对象并传递到OtherActivity,然后在OtherActivity中成功接收并打印该对象的过程。实验证明,数据成功传递并正确显示在控制台。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目标:单击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());

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值