如何初始化存储在另一个活动的Pojo类中的arraylist? [英] How to initialize arraylist which store in a Pojo class in another activity?

查看:92
本文介绍了如何初始化存储在另一个活动的Pojo类中的arraylist?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据存储在ArrayList中并在另一个类中访问它,但是当我访问arraylist时,arraylist.size()为0.意味着我没有访问相同的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.

这是我的POJO课

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

这是我将数据存储到 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);

问题出在 B类中,当我访问item.size()时它返回值0,这意味着我没有访问同一数组列表.

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 中的数组列表的正确方法是什么?

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

对于破裂通行证,例如:

For fragement pass like:

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类中的arraylist?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆