使用parcablearraylist将arraylist <customobject>从Activity传递给varioud片段

时间:2016-11-14 08:21:43

标签: android android-fragments arraylist

我想将自定义对象的Arraylist从我的活动传递给我的片段,我试图通过Parcelable Arraylist传递它,但我无法做任何人可以帮忙吗?任何传递arraylist的方法都可以从活动到片段

我的代码

-cp

fragment1 oncreateview:检索

 String driverId = jsonObject.getJSONArray("user_data").getJSONObject(count).getString("order_id");
 String pickupaddress = jsonObject.getJSONArray("user_data").getJSONObject(count).getString("pickup_address");
 String contact = jsonObject.getJSONArray("user_data").getJSONObject(count).getString("contact");
 Log.d("HomeAcivity",": "+driverId+","+pickupaddress+","+contact);
singleton = new Singleton(driverId,pickupaddress,contact);
userDataParserArrayList.add(singleton);

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Jobs jobs = new Jobs();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Order",userDataParserArrayList);
jobs.setArguments(bundle);
fragmentTransaction.commit();

ParcableClass()它不是单例类,只有名称是singleton

userDataParserArrayList =  getArguments().getParcelableArrayList("Order");

1 个答案:

答案 0 :(得分:0)

@bipin首先,您需要将片段添加到活动中,如下面的代码

Jobs jobs = new Jobs(); 
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add("JOB_FRAGEMENT", jobs).commit();

创建&#34; 作业片段&#34;您可以从您的活动中访问arrayList,如下面的代码

((YourActivityName)getActivity()).yourArrayList(); in onViewCreated() method;

然后使用此列表!!!