如何在非原始类型上使用 Parcelable? [英] How to use Parcelable on non-primitive types?

查看:61
本文介绍了如何在非原始类型上使用 Parcelable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TableMetaAttrs 类:

I have a class TableMetaAttrs:

public class TableMetaAttrs implements Parcelable
{
    private Integer id;
    private String name;
    private String value;
    private Tables table;
...

我如何在 table 字段上实现 Parcelable?类 Tables 还实现了 Parcelable.

How do i implement Parcelable on table field? Class Tables also implements Parcelable.

推荐答案

你必须让 Tables 类也实现 Parcelable.

这样,每当 TableMetaAttrs 正在打包或重建自身时,它可以为表字段调用 putParcelablegetParcelable.

That way whenever TableMetaAttrs is parcelling or re-building itself, it can call putParcelable and getParcelable for the table field.

我假设 Tables 是你创建的一个类,所以它的签名必须是

I'm assuming Tables is a class you created, so it's signature must be

public class Tables implements Parcelable{
    // and somewhere in this code tables have to parcel all it's primitives
}

然后每当 TableMetaAttrs 对它的值进行打包时,它会调用:

then whenever TableMetaAttrs is parcelling it's values it calls:

 parcel.putParcelable(table);

并重建它:

  table = parcel.getParcelable();

这篇关于如何在非原始类型上使用 Parcelable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆