如何写入 Parcelable 中两个或多个自定义对象的数组列表?

How to write to arraylist of two or more custom objects in Parcelable?

我浏览了 Stack 上的问题,没有发现针对特定问题的问题。我有一个带有两个自定义对象数组列表的 class。我现在不确定如何为父 class 实现 writeToParcel。我找到的一个选项是 readTypedList 构造函数,但我无法实现它两次。没有我可以创建数组并发送它的 readTypedListArray 构造函数。

提醒一下,对象是 ArrayList,父对象中有两个这样的对象 class。 github link 供参考

https://github.com/legalimpurity/PopularMoviesStage2/tree/master/app/src/main/java/popularmoviesstage1/legalimpurity/com/popularmoviesstage2/objects

您可以使用以下方式添加自定义数组:

MovieObjectclass中:

  @Expose
  private ArrayList< ReviewObject > ReviewObjs = new ArrayList< ReviewObject >();
  private ArrayList< TrailerVideoObject > TrailerVideoObjs = new ArrayList< TrailerVideoObject >();

writeToParcel() :

 public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(ReviewObjs);
        dest.writeTypedList(TrailerVideoObjs);
    }

和 readFromParcel() :

private void readFromParcel(Parcel in) {

    ReviewObjs = new ArrayList<ReviewObject>();
    in.readTypedList(ReviewObjs, ReviewObject.CREATOR);

    TrailerVideoObjs = new ArrayList<TrailerVideoObject>();
    in.readTypedList(TrailerVideoObjs, TrailerVideoObject.CREATOR);

  }

并在 CustomClasses 中添加以下行:

对于 ReviewObject :

public class ReviewObject implements Parcelable {
    public static final Creator<ReviewObject> CREATOR = new Creator<ReviewObject>() {
        public ReviewObject createFromParcel(Parcel in) {
            return new ReviewObject(in);
        }

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

    };
// add other objects...
}

对于 TrailerVideoObject :

public class TrailerVideoObject implements Parcelable {
    public static final Creator<TrailerVideoObject> CREATOR = new Creator<TrailerVideoObject() {
        public TrailerVideoObject createFromParcel(Parcel in) {
            return new TrailerVideoObject(in);
        }

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

    };
// add other objects...
}

在您的 MovieObject.java 中,您可以将它们声明为 List<YOUR_CLASS> 而不是 ArrayList<YOUR_CLASS>

在我的代码中:

public class CombinedClass implements Parcelable {
    private List<Class1> class1List; 
    private List<Class2> class2List; 
    //Constructor, getter, setter, methods.
}

Class1 和 Class2 都实现了 Parcelable。 我正在使用 "auto-generated" Studio 提供的 Parcelable 实现。 这是 Parcelable 的实现:

protected CombinedClass(Parcel in) {
    class1List = in.createTypedArrayList(Class1.CREATOR);
    class2List = in.createTypedArrayList(Class2.CREATOR);
}
public static final Creator<CombinedClass> CREATOR = new Creator<CombinedClass>() {
   @Override
    public CombinedClass createFromParcel(Parcel in) {
        return new CombinedClass(in);
    }

    @Override
    public CombinedClass[] newArray(int size) {
        return new CombinedClass[size];
    }
};
@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeTypedList(class1List);
    parcel.writeTypedList(class2List);
}

您可以在 MovieObject.java

中尝试以下解决方法
@Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeStringArray(new String[] {
                this.OrignalTitle,
                this.MoviePosterImageThumbnailUrl,
                this.PlotSynopsis,
                this.UserRating,
                this.ReleaseDate
        });

        dest.writeLongArray(new long[] {
                this._id,
                this.ApiId
        });

         ArrayList<ArrayList> temp = new List<>();
         temp.add(ReviewObjs);
         temp.add(TrailerVideoObjs);

         dest.writeTypedList(temp);


    }