将自定义对象的parcelable添加到接口类 [英] adding parcelable to an interface class for custom objects

查看:110
本文介绍了将自定义对象的parcelable添加到接口类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义对象类,但是它是通过一个接口实现的,我该如何将parceable合并到其中. 我关注并搜索了parceable,但这仅适用于对象类. 例如:如何使我的自定义对象可拆分?

I have a custom object class but that is implemented through an inteface, how can i incorporate parceable in it. I have followed and searched about parceable, but it is only for object class. eg : How can I make my custom objects Parcelable?

我想将我的对象列表传递给android中的另一个活动.

I want to pass my object list to another activity in android.

代码:

public interface Projection {

  interface Job {
        @XBRead("./task")
        List<Task> getTasks();

        @XBRead("./id")
        String getid();

        @XBRead("./job_title")
        String getjob_title();

        @XBRead("./job_description")
        String getjob_description();

        @XBRead("./job_room")
        String getjob_room();

        @XBRead("./status")
        String getstatus();
    }

    interface Task {

        @XBRead("./task_id")
        String gettask_id();

        @XBRead("./task_title")
        String gettask_title();

        @XBRead("./task_description")
        String gettask_description();

        @XBRead("./task_status")
        String gettask_status();


    }

    @XBRead("/root/job")
    List<Job> getJobs();
}

推荐答案

您的自定义界面需要extend Parcelable.

实现自定义接口的类还需要实现Parcelable接口,包括CREATOR.

Classes that implement your custom interface need to also implement the Parcelable interface, including the CREATOR.

然后您可以像下面这样向Intent添加实现自定义接口的对象:

You can then add an object implementing your custom interface to an Intent like this:

intent.putExtra("thing", thing);

或添加包含以下对象的ArrayList:

or add an ArrayList containing these objects like this:

ArrayList<Thing> things;
intent.putParcelableArrayListExtra("things", things);


在接收端,Activity可以像这样从Intent中提取对象:


On the receiving end, the Activity can extract the objects from the Intent like this:

Thing thing = intent.getParcelableExtra("thing");

ArrayList<Thing> things = intent.getParcelableArrayListExtra("things");

这篇关于将自定义对象的parcelable添加到接口类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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