问题描述
我正在使用viewpager并创建片段,我想传递arraylist.因此,我尝试了以下操作:
I am using viewpager and creating fragments and i want to pass the arraylist. So i have tried the below things:
MainActivity:
MainActivity:
private ArrayList<customers> mArrayList = null;
ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this.getSupportFragmentManager());
adapter.addFrag(NewCustomer.newInstance(mArrayList ), "NewCustomer");
现在在片段类中,我正在创建实例:
Now in fragment class i am creating instance:
public static final ArrayList<customers> data = new ArrayList<customers>();
public static final NewCustomer newInstance(ArrayList<customers> mArrayList) {
NewCustomer f = new NewCustomer();
Bundle bdl = new Bundle(1);
bdl.putParcelableArrayList(data, mArrayList);
f.setArguments(bdl);
return f;
}
但是这不起作用.我要获取arraylist并将其存储到该类的本地arraylist上的bdl.putParcelableArrayList上显示错误.
But this is not working. It is showing error on bdl.putParcelableArrayList I want to get arraylist and store into the local arraylist of that class.
如何获取带有自定义对象的arraylist?
How can i get the arraylist with my custom object ?
推荐答案
可以尝试吗?
public static final NewCustomer newInstance(ArrayList<customers> mArrayList) {
NewCustomer f = new NewCustomer();
Bundle bdl = new Bundle(1);
this.data = mArrayList; // assign its Value
bdl.putParcelableArrayList(data, mArrayList);
f.setArguments(bdl);
return f;
}
this.data = mArrayList
将为片段的当前成员变量赋值.现在我们可以在当前片段中对其进行访问了.
this.data = mArrayList
will assign value to the current member variable of fragment. Now it can we be accessed in current fragment.
这篇关于如何将自定义对象arraylist从活动传递到带有实例的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!