1. 程式人生 > >Android Bundle傳遞數據

Android Bundle傳遞數據

log ont put serial array text content reat cnblogs

1.傳遞普通數據

                Intent intent=new Intent(MainActivity.this,TwoActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("name","張三");
                bundle.putInt("age",18);
                bundle.putString("gender","男");
                intent.putExtras(bundle);
                startActivity(intent);

獲取傳遞的數據

        Bundle bundle=getIntent().getExtras();
        String name=bundle.getString("name");
        String gender=bundle.getString("gender");
        int age =bundle.getInt("age");    

2.傳遞Serializable數據

1.創建一個類實現Serializable

2.傳遞數據

                Intent intent=new Intent(MainActivity.this
,TwoActivity.class); Bundle bundle=new Bundle(); Person1 p1=new Person1("張三","男",18); bundle.putSerializable("person",p1); intent.putExtras(bundle); startActivity(intent);

3.接受數據

        Bundle bundle=getIntent().getExtras();
        Person1 p1
= (Person1) bundle.getSerializable("person"); String name=p1.getName(); String gender=p1.getGender(); int age =p1.getAge();

3.傳遞Parcelable數據

1.創建類實現Parcelabel

public class Person3 implements Parcelable {
    private String name;
    private String gender;
    private int age;


    public Person3(String name, String gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }


    public String getName() {
        return name;
    }

    public String getGender() {
        return gender;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Person3{" +
                "name=‘" + name + ‘\‘‘ +
                ", gender=‘" + gender + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }



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

        /**
         * 供外部類反序列話本類數組使用
         * @param source
         * @return
         */

        @Override
        public Person3 createFromParcel(Parcel source) {
            return new Person3(source);
        }


        /**
         * 從Parcel中讀取數據
         * @param size
         * @return
         */
        @Override
        public Person3[] newArray(int size) {
            return new Person3[size];
        }
    };

    /**
     * 默認返回0就行
     * @return
     */
    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * 把值寫進Parcel中
     * @param dest
     * @param flags
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(gender);
        dest.writeInt(age);
    }

    /**
     * 這裏的讀取數據必須與writeToParacel(Parcel dest,int flags)一致,否則就會出錯
     * @param source
     */
    public Person3(Parcel source) {
        name = source.readString();
        gender=source.readString();
        age = source.readInt();
    }
}

2.傳遞數據

                Intent intent=new Intent(MainActivity.this,TwoActivity.class);
                Bundle bundle=new Bundle();
                Person3 p3=new Person3("張三","男",18);
                bundle.putParcelable("person",p3);
                intent.putExtras(bundle);
                startActivity(intent);

3.接受數據

     Bundle bundle=getIntent().getExtras();
        Person3 p3= bundle.getParcelable("person");
        String name=p3.getName();
        String gender=p3.getGender();
        int age =p3.getAge();

Android Bundle傳遞數據