Android进阶篇-Parcelable包裹类
Bundle传递数据的时候,我们会发现传递Serializable集合会抛出IO异常。
这个时候我们就必须使用Parcelable包裹类。
Message.java:
public class Message implements Parcelable{ @DatabaseField(generatedId = true,unique=true) int id; @DatabaseField private String Mid;//消息ID private MessageContent Content;//消息内容 @DatabaseField private String Functionid;//消息类型Id @DatabaseField private String Sendtime;//发送时间 @DatabaseField private String Tid;//发送者ID” @DatabaseField private String Tname;//发送者姓名 public String getMid() { return Mid; } public void setMid(String mid) { Mid = mid; } public MessageContent getContent() { return Content; } public void setContent(MessageContent content) { Content = content; } public String getMtypeid() { return Functionid; } public void setMtypeid(String mtypeid) { Functionid = mtypeid; } public String getSendtime() { return Sendtime; } public void setSendtime(String sendtime) { Sendtime = sendtime; } public String getTid() { return Tid; } public void setTid(String tid) { Tid = tid; } public String getTname() { return Tname; } public void setTname(String tname) { Tname = tname; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeString(Mid); dest.writeSerializable(Content); dest.writeString(Functionid); dest.writeString(Sendtime); dest.writeString(Tid); dest.writeString(Tname); } public static final Parcelable.Creator<Message> CREATOR = new Parcelable.Creator<Message>() { @Override public Message createFromParcel(Parcel source) { // TODO Auto-generated method stub Message msg = new Message(); msg.Mid = source.readString(); msg.Content = (MessageContent) source.readSerializable(); msg.Functionid = source.readString(); msg.Sendtime = source.readString(); msg.Tid = source.readString(); msg.Tname = source.readString(); return msg; } @Override public Message[] newArray(int size) { // TODO Auto-generated method stub return null; } }; }
Messages.java:
public class Messages implements Parcelable{ private static final long serialVersionUID = 1L; private String Result;//错误码 0为返回成功 非0为返回失败 private String Message;//错误描述 private List<Message> Messages; public List<Message> getMessages() { return Messages; } public void setMessages(List<Message> messages) { Messages = messages; } public String getResult() { return Result; } public void setResult(String result) { Result = result; } public String getMessage() { return Message; } public void setMessage(String message) { Message = message; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeString(Result); dest.writeString(Message); dest.writeList(Messages); } public static final Parcelable.Creator<Messages> CREATOR = new Parcelable.Creator<Messages>() { @Override public Messages createFromParcel(Parcel source) { // TODO Auto-generated method stub Messages messages = new Messages(); messages.Result = source.readString(); messages.Message = source.readString(); messages.Messages = source.readArrayList(Message.class.getClassLoader()); return messages; } @Override public Messages[] newArray(int size) { // TODO Auto-generated method stub return null; } }; }