How to use Parcelable on non-primitive types?

被刻印的时光 ゝ 提交于 2019-12-24 11:15:09

问题


I have a class TableMetaAttrs:

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

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


回答1:


You have to make the Tables class implements Parcelable as well.

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

edit:

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
}

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

 parcel.putParcelable(table);

and rebuilding it:

  table = parcel.getParcelable();


来源:https://stackoverflow.com/questions/14937009/how-to-use-parcelable-on-non-primitive-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!