Android笔记之Intent传递自定义对象

1、


import java.util.List;
import android.os.Parcel;
import android.os.Parcelable;

/**
* 1)writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中。
* 2)describeContents方法。直接返回0就可以。
* 3)静态的Parcelable.Creator<T>接口,本接口有两个方法:
* createFromParcel(Parcel in) 实现从in中创建出类的实例的功能。
* newArray(int size) 创建一个类型为T,长度为size的数组, returnnew T[size];即可。本方法是供外部类反序列化本类数组使用。
*
* @author JL
*/
public class ViewPoint implements Parcelable {

private String v_name;

private List<String> v_picture;

private List<String> v_description;

/**
* 这一步read的顺序要和writeToParcel的顺序保持一致
* @param parcel
*/
private ViewPoint(Parcel parcel) {
// TODO Auto-generated constructor stub
v_name=parcel.readString();
v_description=parcel.readArrayList(List.class.getClassLoader());
v_picture=parcel.readArrayList(List.class.getClassLoader());
//List<String> :parcel.readArrayList(List.class.getClassLoader())
//Map<String, String> :parcel.readHashMap(Map.class.getClassLoader())
//List<School> :parcel.readArrayList(School.class.getClassLoader())
//School :(School) parcel.readParcelable(School.class.getClassLoader())
}
public ViewPoint() {
// TODO Auto-generated constructor stub
}
public static final Parcelable.Creator<ViewPoint> CREATOR=new Parcelable.Creator<ViewPoint>() {

@Override
public ViewPoint createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new ViewPoint(source);
}

@Override
public ViewPoint[] newArray(int size) {
// TODO Auto-generated method stub
return new ViewPoint[size];
}
};

public String getV_name() {
return v_name;
}
public void setV_name(String v_name) {
this.v_name = v_name;
}
public List<String> getV_picture() {
return v_picture;
}
public void setV_picture(List<String> v_picture) {
this.v_picture = v_picture;
}
public List<String> getV_description() {
return v_description;
}
public void setV_description(List<String> v_description) {
this.v_description = v_description;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
//向Parcel对象写入要序列化的数据
//List一律写成 :writeList
//School :writeParcelable(schoolInfo, arg1),且School本身是Parcelable对象
dest.writeString(v_name);
dest.writeList(v_description);
dest.writeList(v_picture);
}
}

Done

时间: 2024-08-26 19:09:06

Android笔记之Intent传递自定义对象的相关文章

Android 开发笔记——通过 Intent 传递类对象

Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递. Intent中传递这2种对象的方法: Bundle.putSerializable(Key,Object); //实现Serializable接口的对象 Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象 以下

Android 通过 Intent 传递类对象

Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递. Intent中传递这2种对象的方法: Bundle.putSerializable(Key,Object); //实现Serializable接口的对象 Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象 以下

Android中Intent传递Java对象的方法

Android中Intent传递Java对象有两种方法:一是通过调用Bundle对象的putSerializable(Key,Object)方法[参见示例],另一种是通过调用Bundle对象的putParcelable(Key,Object)方法[参见示例]. 第一种方法要求传递的Java对象实现Serializable接口--Serializable接口是JavaSE特有的接口,采用该种方法实现类的序列化非常简单,声明一下系统会自动将其序列化. 第二种方法要求传递的Java对象实现了Parce

android通过 Intent 传递类对象

Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递. Intent中传递这2种对象的方法: Bundle.putSerializable(Key,Object); //实现Serializable接口的对象 Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象 以下

通过 Intent 传递类对象

Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递 Intent中传递这2种对象的方法: Bundle.putSerializable(Key,Object); //实现Serializable接口的对象 Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象 Per

通过Intent传递类对象

一.Intent是什么 1.定义 Intent被译作意图,其实还是很能传神的,Intent期望做到的,就是把实现者和调用者完全解耦,调用者专心将以意图描述清晰,发送出去,就可以梦想成真,达到目的. 这 个解释还是有点不太好理解,下面还有一个:Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android 会根据意愿的内容选择适当的组件来完成请求.比如,有一个Activ

Android笔记:SurfaceView与SurfaceHolder对象

摘要 调试Media播放时,不时用到SurfaceView与SurfaceHolder对象,写case测试及实际运行效果, 基本上搞清楚这两个对象的用法及区别 1.SurfaceView public class SurfaceView extends View SurfaceView是视图(View)的继承类, 这个视图里内嵌了一个专门用于绘制 调试Media播放时,不时用到SurfaceView与SurfaceHolder对象,写case测试及实际运行效果, 基本上搞清楚这两个对象的用法及区

Android开发经验之—intent传递大数据

在Activity或者组件之前传递信息时,一般采用intent绑定bundle的方式传值,但在使用过程中需要注意的是不要用bundle传递大容量数据: 在做项目的过程中,需要将听写界面的听写结果信息传递到听写记录界面供显示用,但是由于传递的数据量过大导致程序ANR,甚至直接报异常(传递的信息里面有bitmap转换成的byte数组.每一个词组的拼音.词语.语音信息),经过分析发现是由于bundle不能传递大容量的数据信息,在stackoverflow里面查阅发现有同行遇到类似的问题: (1)"Th

android笔记6——intent的使用

今天挑出一节专门来说一下使用intent和intentfilter进行通信. 场景:一个Activity启动另一个Activity. 前面已经讲了Fragment的切换,Fragment顾名思义是基于碎片切换的,假如我们要切换屏幕,或者是service组件等等,这就要用到Intent. 此外还想说明一下,Intent还具有很好的设计思想在里面的.它将各种"启动意图"封装成一个一致编程模型,利于高层次的解耦. 1.Intent属性 Component属性 先来看一段代码: <spa