简体   繁体   English

Parcelable类中的Parcelable列表不使用Parcel.readTypedList进行读回

[英]Parcelable list inside Parcelable class not reading back with Parcel.readTypedList

I have two Activities, A and B. I am trying to send object from Activity A, to Activity B. When in Activity A, I can see that my List contains two items, but when I retrieve it in Activity B, the List contains 7000000+ records. 我有两个活动A和B。我试图将对象从活动A发送到活动B。在活动A中,我可以看到我的列表包含两个项目,但是在活动B中检索它时,列表中包含7000000+条记录。

Here is my Assessment class, that implements Parcelable , and contains an ArrayList<Photo> which should be parcelable as well. 这是我的评估类,实现了Parcelable ,并且包含一个ArrayList<Photo>也应该是可拆分的。

Assessment POJO: 评估POJO:

public class Assessment extends BaseObservable implements Parcelable {
    public Assessment(){

    }

    @SerializedName("Vehicle")
    private String vehicle;

    @SerializedName("Photos")
    private List<Photo> photos;

    @Bindable
    public String getVehicle() {
        return vehicle;
    }
    public void setVehicle(String vehicle) {

        this.vehicle = vehicle;
        notifyPropertyChanged(BR.vehicle);
    }

    public List<Photo> getPhotos() {
        return photos;
    }
    public void setPhotos(List<Photo> photos) {

        this.photos = photos;
    }

    protected Assessment(Parcel in) {
        vehicle = in.readString();
        photos = new ArrayList<Photo>();
        in.readTypedList(photos, Photo.CREATOR);
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(vehicle);
        dest.writeTypedList(photos);
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<Assessment> CREATOR = new Parcelable.Creator<Assessment>() {
        @Override
        public Assessment createFromParcel(Parcel in) {
            return new Assessment(in);
        }

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

Photo POJO: 图片POJO:

public class Photo implements Parcelable {

    public Photo(){

    }

    @SerializedName("PhotoPath")
    private String photoPath;
    public String getPhotoPath() {
        return photoPath;
    }
    public void setPhotoPath(String photoPath) {
        this.photoPath = photoPath;
    }

    @SerializedName("Base64PhotoString")
    private  String photoBase64String;
    public String getPhotoBase64String() {
        return photoBase64String;
    }
    public void setPhotoBase64String(String photoBase64String) {
        this.photoBase64String = photoBase64String;
    }

    protected Photo(Parcel in) {
        photoPath = in.readString();
        photoBase64String = in.readString();
    }

    //region parelable
    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(photoPath);
        dest.writeString(photoBase64String);
    }
    @SuppressWarnings("unused")
    public static final Parcelable.Creator<Photo> CREATOR = new Parcelable.Creator<Photo>() {
        @Override
        public Photo createFromParcel(Parcel in) {
            return new Photo(in);
        }

        @Override
        public Photo[] newArray(int size) {
            return new Photo[size];
        }
    };
    //endregion
}

Here is how I send the object via Intent from Activity A, to Activity B: 这里是我经由发送的对象Intent从活动A,到活动B:

public void OnAdapterItemClicked(View view){
        Intent activityIntent = new Intent(this, com.example.andrewkp.gaassessing.DisplayAssessment.class);
        Assessment extraAssessment = getAssessmentFromCollection(view); //extraAssessment.getPhotos().size() == 2
        activityIntent.putExtra("assessment", extraAssessment); 
        startActivity(activityIntent);
    }

And here is how I read the Parcelable object in Activity B: 这是我在活动B中读取Parcelable对象的方式:

assessment = getIntent().getExtras().getParcelable("assessment");

I have looked at the following article , and I follow exactly what they do, but my photos list does not persist through to Activity B: 我看了下面的文章 ,并且完全按照他们的操作去做,但是我的照片列表并没有持续到活动B:

When I debug the readTypedList method in Parcel class, I can see that it adds 7000000+ records to my ArrayList, but never removes them. 当我调试Parcel类中的readTypedList方法时,我可以看到它向ArrayList中添加了7000000+条记录,但从未删除它们。 Why is this behavior happening? 为什么会发生这种现象?

You are able to put up to 1MB of data in a Bundle encapsulated inside Intent . 您最多可以将1MB数据放入封装在Intent内的Bundle

You will get bunch of errors when sending PhotoBase64String in Bundle . Bundle发送PhotoBase64String时会出现一堆错误。

However, in order to overcome this issue, I would suggest path/URI of your photo to your second activity. 但是,为了解决此问题,我建议您将照片的路径/ URI推荐给第二个活动。 Then in your second activity, read photo from that path, and perform your desired operation. 然后在第二个活动中,从该路径读取照片,然后执行所需的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM