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
- Parcelable 复杂对象,对象列表等
- Parcelable传递图片,复杂对象,对象列表
- Parcelable传递图片,复杂对象,对象列表
- Intent传递复杂ArrayList对象(Parcelable)
- Android通过Parcelable传递复杂的对象数据
- intent 传递复杂对象,对象列表,各种东西,相当完整
- axis 传输 简单对象,复杂对象,List,Map等
- axis 传输 简单对象,复杂对象,List,Map等收藏
- Parcelable序列化对象
- 对象序列化 parcelable
- Android学习札记14:利用Parcelable在Activity间传递复杂的嵌套ArrayList对象参数
- Android APP--通过Parcelable机制传递复杂的对象
- 如何序列化Control等复杂类型对象
- SpringMVC数据绑定全面示例(复杂对象,数组等)
- SpringMVC数据绑定全面示例(复杂对象,数组等)
- SpringMVC数据绑定全面示例(复杂对象,数组等)
- Android结合业务请求JavaBean、List<JavaBean>等复杂对象
- Android-Http请求JavaBean、List、Map等复杂对象
- gradle老问题
- Android4.0窗口机制和创建过程分析
- Android实现静默安装与卸载
- Hadoop经典案例Spark实现(六)——求最大的K个值并排序
- java中的Cipher类
- Parcelable 复杂对象,对象列表等
- tcpdump filters for HTTP GET & HTTP POST
- 在iPhone5以上机器显示iPhone4尺寸的launchImage
- 算法洗脑系列(8篇)——第二篇 递归思想
- <九度 OJ>题目1526:朋友圈
- linux性能测试命令
- iOS 强制传参 NSInvocation
- loadrunner---MD5加密
- Eclipse导入代码@Override和代码排列缩进出问题