Android Parcelable - Write and read ArrayList< IA > when IA is a interface

28,739

Solution 1

    @SuppressWarnings("unchecked")
public D (Parcel in) {
    list = new ArrayList<IA>();
    (...)    
    //ERROR -> list = (ArrayList<IA>) in.readSerializable 
    list = in.readArrayList(IA.class.getClassLoader());
}

@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
    public D createFromParcel(Parcel in) {
        return new D(in);
    }

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

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    (...)
    dest.writeList(list);
}

Solution 2

I recommend you to install "Android Parcelable code generator" plugin. After this you can generate writeToParcel function, constructor which reads from parcel and creator class only whith clicking Alt+Insert buttons.

Share:
28,739

Related videos on Youtube

Pedro Bernardo
Author by

Pedro Bernardo

Updated on October 14, 2020

Comments

  • Pedro Bernardo
    Pedro Bernardo over 3 years

    I have a interface IA and class B and C that implement them. Both B and C implement Parcelable as well.

    Then I have the tricky part:

    Class D has a ArrayList< IA >. I need this too insert both classes B and C in the arraylist. They share the same structure but the "IS-A" relation don't apply.

    I need to pass D from one activity to another as a Parcel.

    I've tried to write (ArrayList<IA>) in.readSerializable but I got a IOException. I know that if IA was not a interface the problem was easy, but I can't seem to find an easy solution for this.

    Any ideas?

    @SuppressWarnings("unchecked")
    public D (Parcel in) {
        list = new ArrayList<IA>();
        (...)    
        list = (ArrayList<IA>) in.readSerializable 
        }
    
    @SuppressWarnings("rawtypes")
    public static final Parcelable.Creator CREATOR =
    new Parcelable.Creator() {
        public D createFromParcel(Parcel in) {
            return new D(in);
        }
    
        public D[] newArray(int size) {
            return new D[size];
        }
    };
    
    public int describeContents() {
        return 0;
    }
    
    public void writeToParcel(Parcel dest, int flags) {
        (...)
        dest.writeList(list);
    }
    
    • njzk2
      njzk2 about 11 years
      IA should extend Parcelable
    • Pedro Bernardo
      Pedro Bernardo about 11 years
      and D use in.readArrayList(list, Creator)? but which creator?
    • Brosa
      Brosa about 11 years
      Could you post any code with class D's Parcelable method's to take a look.
    • Pedro Bernardo
      Pedro Bernardo about 11 years
      I've edited the question with the solution (don't have enough reputation yet).
    • njzk2
      njzk2 about 11 years
      agreed with your solution, except this allows an implementation of IA that is not parcelable of serializable, hence crashing when you writeList.
    • Pedro Bernardo
      Pedro Bernardo about 11 years
      hum I see where you are going. But extending parcelable on IA won't do the trick. Any suggestion?
    • Eugen Pechanec
      Eugen Pechanec over 7 years
      Here's a prime example why you use Cats and Dogs in examples. Jesus Christ, who's supposed to read this alphabet mess. To the poor soul who stumbles here: medium.com/@kcoppock/…
  • Gem
    Gem almost 10 years
    i have one problem i think you can help. I have ArrayList<HashMap<String, Boolean>> which i want to write to parcel and then read it in next activity. Can you please guide how to do that ?
  • Pedro Bernardo
    Pedro Bernardo almost 10 years
  • Terry
    Terry over 4 years
    How to make sure that the "unchecked" warnings are not there? (Except suppressing them)