pojo类中list存储其他字段_如何初始化存储在另一个活动的Pojo类中的arraylist?

本文详细介绍了如何在Android中使用Parcelable接口进行数据传递,从A类到B类,再到Fragment之间的数据交互,包括ArrayList中Item对象的序列化和反序列化过程。示例代码展示了如何创建Parcelable实现的Item类,以及在Intent和Bundle中操作Parcelable列表的方法。
摘要由CSDN通过智能技术生成

小编典典

像这样更改类:

public class Item implements Parcelable{

private String name;

private String body;

private String profileImage;

public Item(){

}

public Item(Parcel in) {

name = in.readString();

body = in.readString();

profileImage = in.readString();

}

@Override

public int describeContents() {

return 0;

}

@Override

public void writeToParcel(Parcel dest, int flags) {

dest.writeString(name);

dest.writeString(body);

dest.writeString(profileImage);

}

@SuppressWarnings("unused")

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

@Override

public Item createFromParcel(Parcel in) {

return new Item(in);

}

@Override

public Item[] newArray(int size) {

return new Item[size];

}

};

public Item(String body,String name,String profileImage){

this.body = body;

this.name = name;

this.profileImage = profileImage;

}

现在处于A类:

ArrayList mDATA = new ArrayList<>();

/****** add values in array list ****/

Intent i = new Intent(CLASS_A.this, CLASS_B.class);

i.putParcelableArrayListExtra("ARRAY_DATA", mDATA);

startActivity(i);

现在在B类中,获取列表:

Intent intent = getIntent();

ArrayList mDATAFROMA = new ArrayList<>();

try {

mDATAFROMA = intent.getParcelableArrayListExtra("ARRAY_DATA");

Log.d("ListSize",String.valueOf(mDATAFROMA.size()));

} catch (Exception e) {

e.printStackTrace();

}

对于破裂通过:

Bundle args = new Bundle();

args.putParcelableArrayList("GET_LIST", (ArrayList extends Parcelable>) mDATA);

fragmentDemo.setArguments(args);

并在片段中获取:

ArrayList mDATAFROMA = new ArrayList<>();

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Bundle pb=getArguments();

mDATAFROMA = pb.getParcelableArrayList("GET_LIST");

}

2020-11-30

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值