How to initialize arraylist which store in a Pojo class in another activity?

会有一股神秘感。 提交于 2019-12-31 06:57:07

问题


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<Item> 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

List<Item>items = 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<Item> 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?


回答1:


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

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

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

And in fragment fetch:

ArrayList<Item> mDATAFROMA = new ArrayList<>(); 

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle pb=getArguments();
        mDATAFROMA = pb.getParcelableArrayList("GET_LIST"); 
    }


来源:https://stackoverflow.com/questions/43424860/how-to-initialize-arraylist-which-store-in-a-pojo-class-in-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!