Android Parcelable не передает все данные, когда у Custom Model есть дочерний элемент, представляющий собой список типа Custom Model - PullRequest
0 голосов
/ 29 ноября 2018

У меня есть модель ContentDto, которая, помимо других переменных, содержит список типа ContentDto.Когда я пытаюсь передать его в действие с помощью Parcelable, я получаю все данные, кроме дочернего списка типа ContentDto (который имеет значение null).

В моем адаптере:

val nextIntent = Intent(context, DynamicBaseActivity::class.java)
nextIntent.putExtra(Constants.DYNAMIC_DETAIL, contentDto)
context.startActivity(nextIntent)

В получающем действии:

val bundle = intent.extras
if (bundle != null) {
    contentDto = bundle.getParcelable(Constants.DYNAMIC_DETAIL) as ContentDto
}

My ContentDto:

public class ContentDto implements Parcelable {

    @SerializedName("dataType")
    String dataType;
    @SerializedName("label")
    String label;
    @SerializedName("value")
    String value;
    @SerializedName("icon")
    String icon;
    @SerializedName("isMandatory")
    Boolean isMandatory;
    @SerializedName("isEditable")
    Boolean isEditable;
    @SerializedName("attributeValues")
    List<AttributeModel> attributeValues;
    @SerializedName("tableDto")
    TableDto tableDto;
    @SerializedName("contentDto")
    List<ContentDto> contentDto;    // This comes as null

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(dataType == null ? "" : dataType);
        dest.writeString(label == null ? "" : label);
        dest.writeString(value == null ? "" : value);
        dest.writeString(icon == null ? "" : icon);
        dest.writeInt((isMandatory == null ? 0 : isEditable ? 1 : 2));
        dest.writeInt((isEditable == null ? 0 : isMandatory ? 1 : 2));
        dest.writeParcelable(tableDto == null ? new TableDto() : tableDto, flags);
        dest.writeList(attributeValues == null ? new ArrayList<AttributeModel>(0) : attributeValues);
        dest.writeList(contentDto == null ? new ArrayList<ContentDto>(0) : contentDto);
    }

    @SuppressWarnings("unused")
    public ContentDto(Parcel in) {
        this();
        readFromParcel(in);
    }

    public ContentDto() {
        super();
    }

    private void readFromParcel(Parcel in) {
        this.dataType = in.readString();
        this.label = in.readString();
        this.value = in.readString();
        this.icon = in.readString();
        switch (in.readInt()) {
            case 0:
                this.isMandatory = null;
                break;
            case 1:
                this.isMandatory = true;
                break;
            case 2:
                this.isMandatory = false;
                break;
        }
        switch (in.readInt()) {
            case 0:
                this.isEditable = null;
                break;
            case 1:
                this.isEditable = true;
                break;
            case 2:
                this.isEditable = false;
                break;
        }
        this.tableDto = in.readParcelable(TableDto.class.getClassLoader());
        in.readList(new ArrayList<AttributeModel>(), AttributeModel.class.getClassLoader());
        in.readList(new ArrayList<ContentDto>(), ContentDto.class.getClassLoader());
    }

    public static final Parcelable.Creator<ContentDto> CREATOR = new Parcelable.Creator<ContentDto>() {
        public ContentDto createFromParcel(Parcel in) {
            return new ContentDto(in);
        }

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

    // Getters and Setters
}

(AttributeModel, TableDto и их дочерние элементы также являются Parcelable)

...