16

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;
 }
Community
  • 1
  • 1
hectichavana
  • 1,436
  • 13
  • 41
  • 71
  • Solution inside: http://stackoverflow.com/questions/4049627/parcelable-and-inheritance-in-android – Andreas Dolk May 02 '11 at 08:04
  • thanks! So in my case, I only need the 'public abstract class A implements Parcelable' right? I don't need the 'public class B extends A', am I correct? – hectichavana May 02 '11 at 08:13
  • One thing not mentioned is that if you have data already as a JSON/XML object and if you already have the parser written, it's sometimes easier to just pass the JSON or XML object if you aren't passing it more than 1-2 times – Joe Plante Oct 11 '12 at 14:51
  • Separate comment/idea: also, some of these objects get pretty heavy. Writing yourself a code generator to make an object Parcelable I feel can be a good time-saver in the long run – Joe Plante Oct 11 '12 at 14:55

1 Answers1

17

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.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • thank you! One more thing, I got the 'Project' class which holds the JSON variable, how can I define the 'Project' in my ConstantData Parcelable class? For example, on the code above I put the Project inside an array. "public static ArrayList projectsList = new ArrayList();" – hectichavana May 02 '11 at 08:48
  • @Irzan: You mean how to write that list to the parcel? You can use the http://developer.android.com/reference/android/os/Parcel.html#writeList(java.util.List) method for this. – Heiko Rupp May 02 '11 at 08:53
  • can you provide me a concrete example as well please? Thanks a lot :) – hectichavana May 02 '11 at 09:04
  • I tried to write 'projectsList = parcel.readList();' inside private 'Objects(Parcel parcel)' it can't be done unfortunately. it asked me (List,ClassLoader) for the arguments – hectichavana May 02 '11 at 09:55
  • If you are doing Parcels with a Parcel, try doing the readTyped... functions. These especially help if you implemented CREATOR – Joe Plante Oct 11 '12 at 14:50