栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何初始化存储在另一个活动的Pojo类中的arraylist?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何初始化存储在另一个活动的Pojo类中的arraylist?

像这样更改类:

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<>();     Intent i = new Intent(CLASS_A.this, CLASS_B.class);     i.putParcelableArrayListExtra("ARRAY_DATA", mDATA);     startActivity(i);

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418602.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号