Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding parcelable to an interface class for custom objects

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?

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

code :

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();
}
like image 479
Hiteshdua1 Avatar asked Feb 08 '15 10:02

Hiteshdua1


People also ask

How do I add a Parcelable?

Create Parcelable class without plugin in Android Studioimplements Parcelable in your class and then put cursor on "implements Parcelable" and hit Alt+Enter and select Add Parcelable implementation (see image). that's it.

How do you make a class Parcelable in Java?

If a developer wants to convert a Java object into Parcelable, then the best way to do so is by implementing the Parcelable interface and overriding the writeToParcel() methods in its own class. The first step is to override the writeToParcel() method and write all object members into parcel objects.

Can we use Parcelable in Java?

Serializable is also an interface and it's not a part of Android SDK. Parcelable is the Android implementation of Java serializable. Parcelable is relatively fast as compared to the java serialization. It's recommended to use Parcelable.

What is a primary purpose of the Parcelable interface?

The Parcelable interface adds methods to all classes you want to be able to transfer between activities. These methods are how parcelable deconstructs the object in one activity and reconstructs it in another. For this example you'll look at how to implement parcelable in a simple class.


1 Answers

Your custom interfaces need to extend Parcelable.

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

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

intent.putExtra("thing", thing);

or add an ArrayList containing these objects like this:

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

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

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

or

ArrayList<Thing> things = intent.getParcelableArrayListExtra("things");
like image 181
David Wasser Avatar answered Nov 15 '22 16:11

David Wasser