Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to implement Parcelable to my objects? [duplicate]

Tags:

java

android

I have this ConstantData class which holds my JSON indexes, I need to pass them between activities with extras. But before that, I have to implement Parcelable to the objects of this class first.

My question is, how should I declare the object within my class here and put every object inside a variable?

I'm a newbie and I'm totally clueless right now. Thank you. Feel free to modify my code below.

ConstantData.java

public class ConstantData{

       public static String project_title = "project title";
       public static String organization_title = "organization title";
       public static String keyword = "keyword";
       public static String short_code = "short code";
       public static String project_description = "description";
       public static String smallImageUrl = "smallImageUrl";
       public static String bigImageUrl = "bigImageUrl";
       public static String price= "price";
       public static String country= "country";



        public static ArrayList<Project> projectsList = new ArrayList<Project>();


        public int describeContents() {
            return 0;
        }

        public void writeToParcel(Parcel out, int flags) {
            out.writeString(project_title);
            out.writeString(organization_title);
            out.writeString(keyword);
            out.writeString(short_code);
            out.writeString(project_description);
            out.writeString(smallImageUrl);
            out.writeString(bigImageUrl);
            out.writeString(price);
            out.writeString(country);
        }

        public static final Parcelable.Creator<ConstantData> CREATOR
                = new Parcelable.Creator<ConstantData>() {
            public ConstantData createFromParcel(Parcel in) {
                return new ConstantData(in);
            }

            public ConstantData[] newArray(int size) {
                return new ConstantData[size];
            }
        };

        private ConstantData(Parcel in) {
            project_title = in.readString();
            organization_title = in.readString();
            keyword = in.readString();
            short_code = in.readString();
            project_description = in.readString();
            smallImageUrl = in.readString();
            bigImageUrl = in.readString();
            price = in.readString();
            country = in.readString();
        }
    }

In case my question is not clear enough, you can look up this question: How to send an object from one Android Activity to another using Intents?

There he wrote myParcelableObject, I just don't know how to make the parcelable object.

EDIT Project.java

public class Project {

    public String project_title;
    public String organization_title;
    public String keyword;
    public String short_code;
    public String project_description;
    public String smallImageUrl;
    public String bigImageUrl;
    public String price;
    public String country;
 }
like image 396
hectichavana Avatar asked May 02 '11 08:05

hectichavana


People also ask

How do you make an object Parcelable?

Create Parcelable class without plugin in Android Studio implements 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 add a Parcelable implementation?

You can install this plugin by going to Android Studio -> File -> Settings -> Plugins -> Browse repositories : Here are all the Java types it supports: Types implementing Parcelable. Custom support (avoids Serializable / Parcelable implementation) for: Date , Bundle.

Why is it better to use Parcelable than serializable in Android?

Parcel able is faster than serializable. Parcel able is going to convert object to byte stream and pass the data between two activities. Writing parcel able code is little bit complex compare to serialization. It doesn't create more temp objects while passing the data between two activities.

Why do we use Parcelable in Android?

Parcelable and Bundle objects are intended to be used across process boundaries such as with IPC/Binder transactions, between activities with intents, and to store transient state across configuration changes.


1 Answers

You need to implement the Parcelable interface

public class ConstantData implements Parcelable {

    public void writeToParcel(Parcel out, int flags) {
        out.writeString(project_title);
    ....
        out.writeString(country);
        out.writeList(projectsList); // <<--
    }

    private ConstantData(Parcel in) {
        project_title = in.readString();
        ....
        country = in.readString();
        projectsList = in.readList();

I think for the writing of the projectsList to work, the Project class also needs to implement Parcelable.

See e.g. this class for an example.

like image 52
Heiko Rupp Avatar answered Sep 22 '22 19:09

Heiko Rupp