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

时间:2018-05-05 17:37:50

标签: java android android-activity parcelable

我有两个活动,A和B.我正在尝试将对象从活动A发送到活动B.在活动A中,我可以看到我的列表包含两个项目,但是当我在活动B中检索它时,该列表包含7000000多条记录。

这是我的评估类,它实现了 Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click Dim playerName As String Dim jersey As Integer Dim assists As Integer Dim goals As Integer 'variables for the numerous things' Dim gamesWon As Integer Dim points As Integer = 0 Dim lowestpoints As Integer Dim highestpoints As Integer 'Enter Information here' playerName = InputBox("Enter the Player's Name: - end with x") jersey = InputBox("Enter the Jersey of the Player - end with -1") assists = InputBox("Enter the # of Assists of the Player - end with -1") goals = InputBox("Enter the # of Goals - end with -1") gamesWon = InputBox("Enter the # of Games Won - end with a -1") 'do while name is not "X" and "-1" While playerName <> "x" And jersey <> "-1" And assists <> "-1" And goals <> "-1" And gamesWon <> "-1" points = goals * 2 + assists 'display the stats' txtPlayerNames.Text = txtPlayerNames.Text + playerName + vbNewLine txtJerseys.Text = txtJerseys.Text + Str(jersey) + vbNewLine txtAssists.Text = txtAssists.Text + Str(assists) + vbNewLine txtGoals.Text = txtGoals.Text + Str(goals) + vbNewLine txtGamesWon.Text = txtGamesWon.Text + Str(gamesWon) + vbNewLine txtPoints.Text = txtPoints.Text + Str(points) + vbNewLine txtHighestPoints.Text = txtHighestPoints.Text + Str(highestpoints) + vbNewLine 'ask user for playernames and stats - end the input with the name "X" playerName = InputBox("Enter the Player's Name: - end with x") jersey = InputBox("Enter the Jersey of the player - end with -1") assists = InputBox("Enter # of assists of the player - end with -1") goals = InputBox("Enter the # of Goals - end with -1") gamesWon = InputBox("Enter the # of Games Won - end with -1") 'ask for players names and stats and ends with -1' 'if not equal to these integers it exits the loop. If playerName = "x" Or jersey = "-1" Or assists = "-1" Or goals = "-1" Or gamesWon = "-1" Then Exit While End If End While ,并且包含一个Parcelable,它也应该是可以包含的。

评估POJO:

ArrayList<Photo>

照片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];
        }
    };
}

以下是我通过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 } 从活动A向活动B发送对象的方式:

Intent

以下是我如何阅读活动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); } 对象:

Parcelable

我查看了以下article,并且我完全按照他们的操作行事,但我的照片列表并未持续到活动B:

当我在Parcel类中调试readTypedList方法时,我可以看到它向我的ArrayList添加了7000000条记录,但从未删除它们。为什么会发生这种情况?

1 个答案:

答案 0 :(得分:2)

您可以在Bundle封装在Intent内的 1MB 数据。

PhotoBase64String发送Bundle时,您会收到大量错误。

但是,为了解决这个问题,我建议你的照片的路径/ URI 到你的第二个活动。然后在第二个活动中,从该路径中读取照片,然后执行所需的操作。

相关问题