通过Intent传递后,对象列表为null。 Object类实现Parcelable接口

时间:2014-11-10 13:10:10

标签: 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");

此活动接收空列表。我不知道这意味着什么,这里有什么不对。

1 个答案:

答案 0 :(得分:1)

尝试实现Parcelable所需的方法来读写类字段,Parcelable.Creator从parcel和parcel生成parcelable类的实例也是读写类字段值。

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();
    }
}