Intent Parcelable 传递ArrayList使用方法

1.定义我的类并实现Parcelable 接口

定义变量

private Long id;
/** Not-null value. */
private String Name;
private long Type;
/** Not-null value. */
private String MatchTimes;
private boolean Shield;
private boolean isSelected;
重构Setter Getter

private Substances(Parcel in) {
    id=in.readLong();
    Name = in.readString();
    Type = in.readLong();
    MatchTimes = in.readString();
    Shield=in.readByte()!=0;
    isSelected = in.readByte() != 0;
}
重写这些方法

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeLong(id);
    dest.writeString(Name);
    dest.writeLong(Type);
    dest.writeString(MatchTimes);
    dest.writeByte((byte) (Shield ? 1 : 0));
    dest.writeByte((byte) (isSelected ? 1 : 0));
}
public static final Creator<Substances> CREATOR = new Creator<Substances>() {
    @Override
    public Substances createFromParcel(Parcel in) {
        return new Substances(in);
    }

    @Override
    public Substances[] newArray(int size) {
        return new Substances[size];
    }
};
写完之后看看怎么传递哈

public void editClick(View view) {
    Intent intent = new Intent(this, EditActivity.class);
    Bundle bundle=new Bundle();
    bundle.putParcelableArrayList("sub", (ArrayList<? extends Parcelable>) this.substancesList);
    intent.putExtra("bundle", bundle);
    startActivity(intent);
}
在看看怎么接收

Bundle bundleObject = getIntent().getParcelableExtra("bundle");
if(bundleObject!=null)
{
    substancesList=bundleObject.getParcelableArrayList("sub");
    Log.i("EditActivity", "substanceList size:"+substancesList.size());
}
就这样吧 哈哈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值