android - 对象列表通过 Intent 传递后为空。对象类实现 Parcelable 接口(interface)

原文 标签 android android-intent parcelable

这是我从 Activity 传递数据的方式:

   Intent intent = new Intent(JobFieldAttribute.this, JobCamera.class);
   intent.putParcelableArrayListExtra("FieldDataList", fieldDataList);
   startActivity(intent);

我的对象类:
public void writeToParcel(Parcel dest, int flags) {
//Writing to parcel
}   

public static final Parcelable.Creator<FieldData> CREATOR = new Parcelable.Creator<FieldData>() {
    @Override
    public FieldData createFromParcel(Parcel in) {
        FieldData fieldData = new FieldData();
        fieldData.id = in.readInt();
        Log.i("Pracel in", ">> " + in.readString());
        fieldData.value = in.readString();
        fieldData.job_transaction_id = in.readInt();
        fieldData.field_attribute_master_id = in.readInt();           
        fieldData.required = in.readByte() != 0;
        fieldData.viewType = in.readString();
        fieldData.viewLabel = in.readString();
        fieldData.viewSubLabel = in.readString();
        fieldData.viewHelpText = in.readString();
        return fieldData;
    }

    @Override
    public FieldData[] newArray(int size) {
        Log.v("Array size", ">>> "+ size);
        return new FieldData[size];
    }
};

我接收数据的方式:
Intent i = getIntent();
ArrayList<FieldData> fieldDataList =  i.getParcelableArrayListExtra("FieldDataList");

此 Activity 接收空列表。我不知道这意味着什么,这里有什么问题。

最佳答案

尝试实现 Parcelable 所需的方法来读取和写入类字段,Parcelable.Creator 从 Parcel 生成 Parcelable 类的实例,并且 Parcel 还读写类字段值。

public class FieldData implements Parcelable {
    private int id;
    private String value;
    private int job_transaction_id;
    private int field_attribute_master_id;
    private Byte required;
    private String viewType;
    private String viewLabel;
    private String viewSubLabel;
    private String viewHelpText;

    public int describeContents() {

        return 0;
    }

    public void writeToParcel(Parcel out, int flags)
    {
        out.writeInt(id);
        out.writeString(value);
        out.writeInt(job_transaction_id);
        out.writeInt(field_attribute_master_id);
        out.writeByte(required);
        out.writeString(viewType);
        out.writeString(viewLabel);
        out.writeString(viewSubLabel);
        out.writeString(viewHelpText);
    }

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

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

    private FieldData(Parcel in) {

        id = in.readInt();
        value = in.readString();
        job_transaction_id = in.readInt();
        field_attribute_master_id = in.readInt();
        required = in.readByte();
        viewType = in.readString();
        viewLabel = in.readString();
        viewSubLabel = in.readString();
        viewHelpText = in.readString();
    }
}

关于android - 对象列表通过 Intent 传递后为空。对象类实现 Parcelable 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26844633/

相关文章:

java - Uiautomatorviewer文件过早结束

android - Android:如何调用媒体播放器

java - 如何将 Bitmap 对象从一个 Activity 传递到另一个 Activity

android - 创建 Parcelable 的数据类

android - Android-从图库中选择照片

android - 上下文操作栏不覆盖工具栏

java - 如何显示抬头通知android

android - 使用 Parcelable 将包含对象列表的对象传递给另一个 Activity

android - Activity 的荒谬行为

android - 写入时读取包裹不起作用