Parcelable接口的使用
来源:互联网 发布:左耳最后说了什么 知乎 编辑:程序博客网 时间:2024/06/14 17:32
在Android开发中,我们经常需要传递数据,但是,在传递时候,如果我们需要传递的数据类型是自定义的数据类型,我们就需要将其序列化之后再进行传递。
Android中序列化对象有两种方法
1、实现Serializable接口,实现Serializable接口是JavaSE本身就支持的,该方法只需要将自定义数据类型实现该接口即可;
2、实现Parcelable接口,Parcelable是Android特有的功能,效率要高于Serializable接口,我们在使用Intent传递数据的时候,就会发现在Intent的传递数据的方法中就有传递Parcelable序列化的数据类型,而且我们利用AIDL进行进程间通讯的时候,自定义类型的数据也需要实现Parcelable接口才可以。
我们利用一下例子来简单说明一下Parcelable接口的使用:
首先,自定义数据类型,实现Parcelable接口
实现Parcelable接口后,需要实现以下的抽象方法:
1、public int describeContents():该方法直接返回0即可;
2、public void writeToParcel(Parcel dest, int flags):该方法主要用于将自定义数据类型的对象写入Parcel中,即打包传递需要传递的数据到Parcel容器中保存,以便从Parcel中读取数据;
3、静态接口Parcelable.Creator,即private static class InnerCreator implements Parcelable.Creator< Student>,并且创建该类的对象CREATOR,对象名必须为CREATOR,即public static final Parcelable.Creator< Student> CREATOR = new InnerCreator() ;实现该接口以后,需要实现两个抽象方法:
createFromParcel(Parcel source):从Parcel中读取数据
newArray(int size):该方法返回数据类型的size即可;方法是供外部类反序列化本类数组使用;
以下为具体的代码实现:
1、自定义数据类型,实现Parcelable接口
public class Student implements Parcelable { private int id ; private String name ; private String from ; public Student() { } public Student(Parcel source) { readFromParcl(source) ; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } @Override public int describeContents() { return 0; } /** * 该方法中必须按成员变量生命的顺序封装数据,否则会出现获取数据出错 */ @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(id) ; dest.writeString(name) ; dest.writeString(from) ; } /** * 这个方法中的读取的顺序必须和writeToParcel()方法中的顺序是一致的 * 该方法并不是重写的,而是我们自己定义的 * 用于从Parcel中读取数据 * 我们是仿照writeToParcel()方法写的 */ public void readFromParcl(Parcel source) { this.id = source.readInt() ; this.name = source.readString() ; this.from = source.readString() ; } public static final Parcelable.Creator<Student> CREATOR = new InnerCreator() ; /** * 必须实现该接口,该接口实现了从Parcel容器中读取数据,并返回该对象 */ private static class InnerCreator implements Parcelable.Creator<Student> { @Override public Student createFromParcel(Parcel source) { return new Student(source) ; } @Override public Student[] newArray(int size) { return new Student[size]; } }}
2、在MainActivity中封装数据,并传递到SecondActivity中
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // 封装数据 Student student = new Student() ; student.setId(1) ; student.setName("Lily") ; student.setFrom("ShangHai") ; // 利用Intent传递自定义类型的数据 Intent intent = new Intent(this, SecondActivity.class) ; intent.putExtra("student", student) ; startActivity(intent) ; }}
3、在SecondActivity中接受数据,并打印
public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // 获取数据 Student stu = getIntent().getParcelableExtra("student") ; // 打印获取的数据结果 System.out.println("studnet->id = " + stu.getId()) ; System.out.println("student->name = " + stu.getName()) ; System.out.println("student->from = " + stu.getFrom()) ; }}
在运行程序之后,可以在Logcat中发现输出的内容:
以上代码与解释为我自己个人的理解,大家在阅读的时候可以自己一边去敲代码一边去学习,当发现有报错的时候,去找错误发生在哪里,然后自己动手去解决,这样就会记忆深刻。
其实Parcelable序列化接口并不难,我们可以将其理解为在传递数据的时候,我们自定义的数据类型是无法打包传递的,我们将其变为可序列化的,就是将它打包封装到一个容器中,然后在该容器中去读取我们要传递的数据,也就是相当于有个中间的桥梁作用,方便我们数据的传递。
以上内容如果解释不合理的地方,希望各位留言提出宝贵的意见,大家一起进步。
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- Parcelable接口的使用
- 29 Foundation框架之NSURL
- 30 Foundation框架之NSKeyedArchiver和NSKeyedUnarchiver
- EventBus使用详解
- HDU-2086(推一下公式)
- mac androidstudio 无法连接魅蓝手机进行调试
- Parcelable接口的使用
- 监听器(Listener)学习
- LeetCode OJ 系列之118 Pascal's Triangle --Python
- LeetCode OJ 系列之73 Set Matrix Zeroes --Python
- LeetCode OJ 系列之66 Plus One --Python
- 多图片平均法降噪
- LeetCode OJ 系列之65 Valid Number --Python
- LeetCode OJ 系列之58 Length of Last Word --Python
- LeetCode OJ 系列之75 Sort Colors --Python