Parcelable 复杂对象,对象列表等

来源:互联网 发布:刘双军网络二胡教学15 编辑:程序博客网 时间:2024/05/01 16:42

在Android开发过程中,经常要在Activity之间传递参数,使用Android系统提供的方法可以传递基本数据类型的变量,但有时候我们经常要传递一些复杂的数据类型或自定义的类,这种情况的参数无法直接传递,我们可以通过序列化实现</span>
public abstract class A implements Parcelable {  
private int a; 

}
B 继承 A

public class B extends A {  
    private int b;  
}

ParceList implements Parcelable {
public int myInt = 0;
public String str = null;
public float[] flot;
punlic  stringList = new ArrayList<>();
public B b;
public List<B> arrList = new ArrayList<B>();

}

以下为详细代码:

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Intent intent = new Intent();ParceList p = new ParceList();ArrayList<B> bL = new ArrayList<B>();bL.add(new B(33,33));p.arrList = bL;p.b = new B(2,2);p.myInt = 55;p.str = "4";p.flot = new float[]{77,88};Bundle bundle = new Bundle();bundle.putParcelable("key", p);intent.putExtras(bundle);intent.setClass(this, TestPaListAty.class);startActivity(intent);}}




public class TestPaListAty extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);System.out.println(getIntent().getParcelableExtra("key").toString());finish();}}



public abstract class A implements Parcelable {     private int a;        protected A(int a) {          this.a = a;      }        public void writeToParcel(Parcel out, int flags) {          out.writeInt(a);      }        protected A(Parcel in) {          a = in.readInt();      }  }  



public class B extends A {      private int b;        public B(int a, int b) {          super(a);          this.b = b;      }        public static final Creator CREATOR = new Creator() {          public B createFromParcel(Parcel in) {              return new B(in);          }            public B[] newArray(int size) {              return new B[size];          }      };        public int describeContents() {          return 0;      }        public void writeToParcel(Parcel out, int flags) {          super.writeToParcel(out, flags);          out.writeInt(b);      }        private B(Parcel in) {          super(in);          b = in.readInt();      }          @Override    public String toString() {    return "b=" + b;    }}  




public class ParceList implements Parcelable {public int myInt = 0;public String str = null;public float[] flot;public List<String> stringList = new ArrayList<>();public B b;public List<B> arrList = new ArrayList<B>();public String getStr() {return str;}public List<String> getStringList() {return stringList;}public void setStringList(List<String> stringList) {this.stringList = stringList;}public float[] getFlot() {return flot;}public void setFlot(float[] flot) {this.flot = flot;}public B getB() {return b;}public void setB(B b) {this.b = b;}public static Parcelable.Creator getCreator() {return CREATOR;}public void setStr(String str) {this.str = str;}public List<B> getArrList() {return arrList;}public void setArrList(List<B> arrList) {this.arrList = arrList;}public int getMyInt() {return myInt;}public void setMyInt(int myInt) {this.myInt = myInt;}ParceList() {}// 按变量定义的顺序读取public ParceList(Parcel in) {myInt = in.readInt();str = in.readString();flot = in.createFloatArray();in.readStringList(stringList);b = in.readParcelable(B.class.getClassLoader());in.readTypedList(arrList, B.CREATOR);}@Overridepublic int describeContents() {return 0;}// 按变量定义的顺序读取@Overridepublic void writeToParcel(Parcel outParcel, int flags) {outParcel.writeInt(myInt);outParcel.writeString(str);outParcel.writeFloatArray(flot);  outParcel.writeStringList(stringList);outParcel.writeParcelable(b, flags);outParcel.writeTypedList(arrList);}public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {@Overridepublic ParceList createFromParcel(Parcel in) {return new ParceList(in);}@Overridepublic ParceList[] newArray(int size) {return new ParceList[size];}};public String toString() {return "-----" + myInt + "-" + arrList.size() + "-" + stringList.size() + "-"+ b.toString() + "-str" + str + "-float[0]" + flot[0];};}


0 0
原创粉丝点击