Anddroid 应用中传递对象,需要对象实现序列化,序列化的方式有两种:
Serializable方式:
package com.zhw.parcebledemo;
import java.io.Serializable;
/**
* Serializable
*/
public class SerializableVo implements Serializable{
private String id;
private String name;
private String img;
public SerializableVo() {
}
public SerializableVo(String id, String img, String name) {
this.id = id;
this.img = img;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.zhw.parcebledemo;
import android.os.Parcel;
import android.os.Parcelable;
/**
* 序列化Parcelable
*/
public class ParceVo implements Parcelable {
private String id;
private String name;
private String img;
public ParceVo() {
}
public ParceVo(String id, String img, String name) {
this.id = id;
this.img = img;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "ParceVo{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", img='" + img + '\'' +
'}';
}
/**
* 序列化实体类
*/
public static final Parcelable.Creator<ParceVo> CREATOR = new Parcelable.Creator<ParceVo>() {
@Override
public ParceVo createFromParcel(Parcel source) {
return new ParceVo(source);
}
@Override
public ParceVo[] newArray(int size) {
return new ParceVo[size];
}
};
@Override
public int describeContents() {
return 0;
}
/**
* 将实体类数据写入Parcel
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
dest.writeString(this.name);
dest.writeString(this.img);
}
protected ParceVo(Parcel in) {
this.id = in.readString();
this.name = in.readString();
this.img = in.readString();
}
}
MainActivity
<span style="color:#333333;">package com.zhw.parcebledemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button btn,btn02;
private List<ParceVo>list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn= (Button) findViewById(R.id.btn01);
btn02= (Button) findViewById(R.id.btn02);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,TwoActivity.class);
Bundle bundle = new Bundle();
ParceVo p=new ParceVo();
p.setName("name2222");
p.setImg("img1111");
p.setId("id3333");
</span><span style="color:#ff0000;"> bundle.putParcelable("key",p);
intent.putExtras(bundle);</span><span style="color:#333333;">
intent.putExtra("type",1);
Log.i("TAG", "onClick: "+p);
startActivity(intent);
}
});
btn02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,TwoActivity.class);
Bundle bundle = new Bundle();
SerializableVo s=new SerializableVo();
s.setName("name Serializable");
s.setImg("img Serializable");
s.setId("id Serializable");
</span><span style="color:#ff0000;"> bundle.putSerializable("seriali",s);
intent.putExtras(bundle);</span><span style="color:#333333;">
intent.putExtra("type",2);
startActivity(intent);
}
});
}
private void initDta(){
List<ParceVo>data=new ArrayList<>();
for (int i=0;i<10;i++){
ParceVo p=new ParceVo();
p.setName("name"+i);
p.setImg("img"+i);
data.add(p);
}
list.clear();
list.addAll(data);
}
}
</span>
Java中使用的是Serializable,而谷歌在Android使用了自定义的Parcelable。
两种序列化方式的区别:
1.在使用内存的时候,Parcelable比Serializable性能高,推荐使用Parcelable类;
2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC;
3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下,这种情况建议使用Serializable。
参考与:http://blog.csdn.net/zuolongsnail/article/details/8703432
完整的Demo下载地址:
http://download.csdn.net/detail/zhang106209/9633848