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

I want to store the data in a ArrayList and access it in another class,but when when I access the arraylist,the arraylist.size() is 0.Means I didn't access the same arraylist. Somebody please tell me what I doing wrong.

Here is my POJO class

public class item {

private String name;

private String body;

private String profileImage;

public Item(){

}

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

this.body = body;

this.name = name;

this.profileImage = profileImage;

}

//Getter and setter

Here is how I store the data in Class A,which I checked,is successfully insert it to the arraylist.

Class A

List items = new ArrayList<>();

Item item = new Item();

item.setBody(body);

item.setName(name);

item.setProfileImage(profileImage);

items.add(item);

The problem is in Class B when I access the item.size() it return value 0,means that I didnt access to the same arraylist.

Here is what I done in Class B

Listitems = new ArrayList<>();

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

I tried this which I done in RecycleView before,but this doesnt work,cause my Class B is a Fragment activity

public Class B(Context mContext, List items) {

this.mContext = mContext;

this.items = items;

}

So what is the correct way for me initialize the arraylist which I save data to in Class A in Class B?

解决方案

change class like this:

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;

}

Now in Class 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);

Now in Class B, get list:

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

}

For fragement pass like:

Bundle args = new Bundle();

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

fragmentDemo.setArguments(args);

And in fragment fetch:

ArrayList mDATAFROMA = new ArrayList<>();

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Bundle pb=getArguments();

mDATAFROMA = pb.getParcelableArrayList("GET_LIST");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值