类实现了Parcelable接口就可以在Intent中传递 只要在类的定义中实现Parcelable接口
代码
1 package entity; 2 3 import android.os.Parcel; 4 import android.os.Parcelable; 5 6 public class Person implements Parcelable { 7 8 private int id; 9 private String name; 10 private String pass; 11 12 public Person() { 13 super(); 14 // TODO Auto-generated constructor stub 15 } 16 17 public Person(int id, String name, String pass) { 18 super(); 19 this.id = id; 20 this.name = name; 21 this.pass = pass; 22 } 23 24 public int getId() { 25 return id; 26 } 27 28 public void setId(int id) { 29 this.id = id; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 public String getPass() { 41 return pass; 42 } 43 44 public void setPass(String pass) { 45 this.pass = pass; 46 } 47 48 @Override 49 public String toString() { 50 return "Person [id=" + id + ", name=" + name + ", pass=" + pass + "]"; 51 } 52 //----------实现内容描述 53 @Override 54 public int describeContents() { 55 // TODO Auto-generated method stub 56 return 0; 57 } 58 //-------------写入到包中 注意 写入顺序 59 @Override 60 public void writeToParcel(Parcel dest, int flags) { 61 // TODO Auto-generated method stub 62 dest.writeInt(id); 63 dest.writeString(name); 64 dest.writeString(pass); 65 66 } 67 // 添加一个静态成员,名为CREATOR,该对象实现了Parcelable.Creator接口 68 public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() { 69 70 @Override 71 public Person[] newArray(int size) { 72 // TODO Auto-generated method stub 73 return new Person[size]; 74 } 75 76 @Override 77 public Person createFromParcel(Parcel source) { 78 // 从Parcel中读取数据,返回person对象 --------------按顺序读 79 return new Person(source.readInt(), source.readString(), 80 source.readString()); 81 } 82 }; 83 84 }
Person
时间: 2024-10-23 08:30:55