Intent传递对象、对象数组

Android有两种方式传递对象,分别让对象实现Serializable接口和Parceable接口。 
用Serializable传递对象:bundle.putSerializable(“key”,object); 接收对象: (Object)getIntent().getSerializableExtra(“key”);

用Parceable传递对象: bundle.putParcelable(“key”,object);接收对象:(Object)getIntent().getParcelableExtra(“key”);

下面举例说明: 
1. 新建Student类实现Serializable接口

public class Student implements Serializable {

    private static final long serialVersionUID = 1L;
    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

2.新建Teacher类实现Parceable接口

public class Teacher implements Parcelable {

    private int id;
    private String department;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    @Override
    public int describeContents() {
        return 0;
    }

    // 注意这里的write顺序跟下面的read顺序一定要一样
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(department);
    }

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

        @Override
        public Teacher createFromParcel(Parcel source) {
            Teacher teacher = new Teacher();
            teacher.id = source.readInt();
            teacher.department = source.readString();
            return teacher;
        }

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

3.使用Serializable传递、接收对象的方法:

private void serializable_pass() {
        Student stu = new Student();
        stu.setId(1000);
        stu.setName("小明");
        Intent intent = new Intent(MainActivity.this,SerializableActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("serial", stu);
        intent.putExtras(bundle);
        startActivity(intent);
    }

Student stu = (Student) getIntent().getSerializableExtra("serial");

4.使用Parceable传递、接收对象的方法:

private void parceable_pass() {
        Teacher teacher = new Teacher();
        teacher.setId(1001);
        teacher.setDepartment("计算机科学与技术");
        Intent intent = new Intent(MainActivity.this, ParceableActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("parceable", teacher);
        intent.putExtras(bundle);
        startActivity(intent);
    }
Teacher teacher = (Teacher) getIntent().getParcelableExtra("parceable");

5.使用Serializable传递、接收对象数组的方法:

private void serializableList_pass() {
        List<Student> list = new ArrayList<>();
        Student stu1 = new Student();
        stu1.setId(1000);
        stu1.setName("小明");
        Student stu2 = new Student();
        stu2.setId(1002);
        stu2.setName("小红");
        list.add(stu1);
        list.add(stu2);
        Intent intent = new Intent(MainActivity.this, ObjectListActivity.class);
        //把list强制类型转换成Serializable类型
        intent.putExtra("objectList", (Serializable) list);
        startActivity(intent);
    }
List<Student> stu = (List<Student>) getIntent().getSerializableExtra(
                "objectList");

6.传递、接收普通数组方法(String int):

private void stringList_pass() {
        ArrayList<String> list = new ArrayList<>();
        list.add("string1");
        list.add("string2");
        Intent intent = new Intent(MainActivity.this, StringListActivity.class);
        intent.putStringArrayListExtra("stringList", list);
        startActivity(intent);
    }

ArrayList<String> list = getIntent().getStringArrayListExtra(
                "stringList");
//传递int型数组
intent.putIntegerArrayListExtra(key, list);
//接收int型数组
list =(ArrayList<Integer>) getIntent().getIntegerArrayListExtra(key);

运行截图: 
这里写图片描述

该博客转载自:
         https://blog.csdn.net/dzq_feixiang/article/details/50934333

猜你喜欢

转载自blog.csdn.net/unfinished_story/article/details/80360885