Android序列化对象传值
package com.czz.gov.model;
import android.os.Parcel;
import android.os.Parcelable;
//序列化第一步是先Parcelable接口
public class Notice implements Parcelable {
private int id;
private int delete_state;
private int read_state;
private String theme;
private String content;
private String date;
public Notice() {
}
public Notice(int id, int delete_state, int read_state, String theme, String content, String date) {
this.id = id;
this.delete_state = delete_state;
this.read_state = read_state;
this.theme = theme;
this.content = content;
this.date = date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getDelete_state() {
return delete_state;
}
public void setDelete_state(int delete_state) {
this.delete_state = delete_state;
}
public int getRead_state() {
return read_state;
}
public void setRead_state(int read_state) {
this.read_state = read_state;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTheme() {
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// 把数据写入Parcel
dest.writeInt(id);
dest.writeInt(delete_state);
dest.writeInt(read_state);
dest.writeString(theme);
dest.writeString(content);
dest.writeString(date);
}
// 3、自定义类型中必须含有一个名称为CREATOR的静态成员,该成员对象要求实现Parcelable.Creator接口及其方法
public static final Parcelable.Creator<Notice> CREATOR = new Parcelable.Creator<Notice>() {
@Override
public Notice createFromParcel(Parcel source) {
// 从Parcel中读取数据
// 此处read顺序依据write顺序
return new Notice(source.readInt(), source.readInt(), source.readInt(), source.readString(), source.readString(), source.readString());
}
@Override
public Notice[] newArray(int size) {
return new Notice[size];
}
};
}
MainActivity JAVA代码:
// 进行页面跳转
Intent intent = new Intent();
intent.putExtra("notice", notice);
intent.setClass(PullToRefreshExpandableListActivity.this, NoticeDetailActivity.class);
PullToRefreshExpandableListActivity.this.startActivity(intent);
接受Activity java代码:
package com.czz.gov.activity;
import com.czz.gov.R;
import com.czz.gov.model.Notice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class NoticeDetailActivity extends Activity {
private TextView mTextViewDetailTime;
private TextView mTextViewDetailTheme;
private TextView mTextViewDetailContent;
private Notice notice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notice_detail);
mTextViewDetailTime = (TextView) findViewById(R.id.mTextViewDetailTime);
mTextViewDetailTheme = (TextView) findViewById(R.id.mTextViewDetailTheme);
mTextViewDetailContent = (TextView) findViewById(R.id.mTextViewDetailContent);
notice = (Notice) getIntent().getParcelableExtra("notice");
try {
mTextViewDetailTime.setText(notice.getDate());
mTextViewDetailTheme.setText(notice.getTheme());
mTextViewDetailContent.setText(notice.getContent());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}