テント転送をバンドルまたはカスタムオブジェクト(シリアライズ転送)を使用します

テント転送をバンドルまたはカスタムオブジェクトを使用するカスタムオブジェクトのシリアル化する必要があります。

オブジェクトの配列を変換する状態を記憶または送信されても​​よい表します。直列化されたオブジェクトは、ネットワークを介して送信することができる、ローカルに格納されてもよいです。シリアライズParcelableモードと方法:2つの方法のシーケンス。

1.Serializable方法
カスタムクラスを持っている場合、シリアル化された平均値である直列化は、二つのフィールドがありPerson.java、名前と年齢である
あなたが書き込みをシリアル化したいが:

public class Person implements Serializable {	
	private String name;
	private int age;
	public String getName(){
		return name;
	}
	public int getAge(){
		return age;
	}
	public void setName(String name){
		this.name = name;
	}
	public void setAge(int age){
		this.age = age;
	}
		
}

セット、物理的方法は、データのクラス生成および割り当てを読み取られ、フォーカスが最初の行で表され得るが直列化クラスは、直列化インターフェイス人を実装する実装、Personオブジェクトは、すべての可能な配列であります

ときにデータ伝送を:
意図:

//发送数据端
Person person =new Person();
person.setName("Tom");
person.setAge(20);
Intent intent = new Intent(this,NewActivity.class);//或初始化其他Intent的构造函数
intent.putExtra("key",person);//key是你定义的键值对的键值,唯一即可。
context.startActivity(intent);
//接收数据端,比如一个Activity:
Person person =(Person)getIntent().getSerializableExtra("key")

バンドル:

//发送数据端
Person person =new Person();
person.setName("Tom");
person.setAge(20);
Bundle bundle = new Bundle();
bundle.putSerializable("key",person);//key是你定义的键值对的键值,唯一即可。

//接收数据端
Person person =(Person)bundle.getSerializable("key");

シリアライズは比較的単純であるが、オブジェクト全体がシリアライズされ、効率がParcelableの方法よりも低いです。

2.Parcelable方法
Parcelableより効率的にシリアル化する方法が、少しより多くのコードの量。
少し複雑な例

public class MainFunctionBean implements Parcelable { //实现Parcelable 接口
    private String title;
    private int bitmapResKey;
    private boolean isLongPressed = false;
  
    public void setTitle(String title) {
        this.title = title;
    }
    
 	 public String getTitle() {
        return title;
    }
    public void setBitmapResKey(int bitmapResKey) {
        this.bitmapResKey = bitmapResKey;
    }

    public int getBitmapResKey() {
        return bitmapResKey;
    }

     public boolean isLongPressed() {
        return isLongPressed;
    }

    public void setLongPressed(boolean longPressed) {
        isLongPressed = longPressed;
    }

    @Override
    public int describeContents() { //默认覆写的方法,不用改
        return 0;
    }

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

        @Override
        public MainFunctionBean createFromParcel(Parcel source) { 
        //重点覆写 的方法,各个字段的声明顺序必须和	writeToParcel方法里的一致
            MainFunctionBean mainFunctionBean = new MainFunctionBean();
            mainFunctionBean.title = source.readString();
            mainFunctionBean.bitmapResKey = source.readInt();
            mainFunctionBean.isLongPressed = source.readByte() != 0;//序列化的数据恢复成boolean类型
            return mainFunctionBean;
        }

        @Override
        public MainFunctionBean[] newArray(int size) {  //默认覆写的方法,把size传进数组
            return new MainFunctionBean[size];
        }
    };

    @Override
    public void writeToParcel(Parcel dest, int flags) { //重点覆写 的方法,需要将本实体类的字段一 一写出进行序列化。
        dest.writeString(title);//title是String类型
        dest.writeInt(bitmapResKey);//bitmapResKey是int类型
        dest.writeByte((byte) (isLongPressed ? 1 : 0));//isLongPressed 是boolean类型,改写成Byte便于序列化
    }

   
}

言葉へのデータ、メソッド、およびほとんどちょうど直列化Serializableの対応を渡すと、Parcelableになったとき

おすすめ

転載: blog.csdn.net/weixin_43115440/article/details/91441980