如何初始化存储在另一个活动中的 Pojo 类中的数组列表? [英] How to initialize arraylist which store in a Pojo class in another activity?
问题描述
我想将数据存储在一个ArrayList中并在另一个类中访问它,但是当我访问arraylist
时,arraylist.size()
是0.意味着我没有访问同一个数组列表.有人请告诉我我做错了什么.
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.
这是我的 POJO 课程
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
这是我如何将数据存储在 Class A 中,我检查了它是否成功地将其插入到数组列表中.
Here is how I store the data in Class A,which I checked,is successfully insert it to the arraylist.
A级
List<Item> items = new ArrayList<>();
Item item = new Item();
item.setBody(body);
item.setName(name);
item.setProfileImage(profileImage);
items.add(item);
问题出在Class B,当我访问item.size()
时,它返回值0,这意味着我没有访问同一个arraylist.
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.
这是我在B班中所做的
List<Item>items = new ArrayList<>();
Log.d("ListSize",String.valueOf(items.size()));
我之前在 RecycleView
中尝试过这个,但这不起作用,因为我的 B 类是 Fragment 活动
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<Item> items) {
this.mContext = mContext;
this.items = items;
}
那么,我在 Class B 中的 Class A 中初始化将数据保存到的 arraylist 的正确方法是什么?
So what is the correct way for me initialize the arraylist which I save data to in Class A in Class B?
推荐答案
像这样改变类:
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<Item> CREATOR = new Parcelable.Creator<Item>() {
@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<Item> 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 类,获取列表:
Now in Class B, get list:
Intent intent = getIntent();
ArrayList<Item> 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<Item> mDATAFROMA = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle pb=getArguments();
mDATAFROMA = pb.getParcelableArrayList("GET_LIST");
}
这篇关于如何初始化存储在另一个活动中的 Pojo 类中的数组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!