3

I am trying to send an object of type MyCustomObject to another activity via an intent. I know, to make the class Parcelable, I should do public class MyCustomObject implements Parcelable, but I am not sure how custom object arrays work in parcelable. Here's what I have got so far.. Also, do I need to make the Search class implements Parcelable too?

Here is my updated answer I now get a null object when I do intent.getParcelableExtra(search). Maybe I am not creating the search array properly?

public class MyCustomObject implements Parcelable{
    public Search search[];

    public MyCustomObject(Parcel in){
        in.readArray(MyCustomObject.class.getClassLoader());
    }

    @Override
    public int describeContents(){
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags){
        dest.writeArray(search)
    }

    public static final Parcelable.Creator<MyCustomObject> CREATOR = new Parcelable.Creator<MyCustomObject>(){
        @Override
        public MyCustomObject createFromParcel(Parcel source){
            return new MyCustomObject(source);
        }

        @Override
        public MyCustomObject[] newArray(int size){
            return new MyCustomObject[size];
        }
    }

    public static class Search implements Parcelable{
        public int rank;
        public String title;
        public String[] imageURL;

        public Search(Parcel in){
            rank = in.readInt();
            title = in.readString();

            String[] url = new String[4];
            in.readStringArray(url);
            url = imageURL
        }

        @Override
        public int describeContents(){
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags){
            dest.writeInt(rank);
            dest.writeString(title);
            dest.writeStringArray(imageURL);
        }

        public static final Parcelable.Creator<Search> CREATOR = new Parcelable.Creator<Search>(){

        @Override
        public Search createFromParcel(Parcel source){
            return new Search(source);
        }

        @Override
        public Search[] newArray(int size){
            return new Search[size];
        }
    }

    }
}
Raunak
  • 6,427
  • 9
  • 40
  • 52

2 Answers2

7

A couple of things. First, your Search object will need to implement Parcelable as well. Next, you need to create a static field called CREATOR that implements Parcelable.Creator<MyCustomObject>. This will have a method called createFromParcel that returns an instance of MyCustomObject. That is where you read data out of a parcel and create a new instance of MyCustomObject. This is essentially the opposite of writeToParcel. This all applies to Search as well since you must make it implement Parcelable. In summary:

  • Make Search implement Parcelable
  • Implement Search.writeToParcel
  • Create a static field in Search called CREATOR that implements Parcelable.Creator<Search>
  • Implement the createFromParcel method of Search.CREATOR
  • Create a static field in MyCustomObject called CREATOR that implements Parcelable.Creator<MyCustomObject>
  • Implement the createFromParcel method of MyCustomObject.CREATOR
michaelg
  • 2,692
  • 1
  • 17
  • 16
  • Thanks for the direction. I've done as you said.. but I'm getting nullpointerexception at in.readStringArray(imageURL); I suppose thats not how I'm suppose to read a String array. Can you quickly have a look at my answer? – Raunak Sep 26 '11 at 20:46
  • I've fixed the above mentioned problem; now I get a null object when i do intent.getParcelableExtra(search). Any ideas? – Raunak Sep 27 '11 at 15:07
  • Not sure what 'search' is. I assume it is a string and that you used it to putExtra on the Intent that you used to start the Activity. Try debugging by adding a breakpoint after you put the MyCustomObject in a Parcel and then again in the next Activity when it is being read out of the Intent. – michaelg Oct 01 '11 at 00:52
1

when you readArray from Parcel, you want to read a Search[] array. Not the MyCustomObject.

    public MyCustomObject(Parcel in){
       in.readArray(MyCustomObject.class.getClassLoader());
    }
haijin
  • 938
  • 2
  • 11
  • 17