我正在使用 viewpager 并创建 fragment ,我想传递数组列表。所以我尝试了以下方法:
主要 Activity :
private ArrayList<customers> mArrayList = null;
ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this.getSupportFragmentManager());
adapter.addFrag(NewCustomer.newInstance(mArrayList ), "NewCustomer");
现在在 fragment 类中我正在创建实例:
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;
}
但这行不通。它在 bdl.putParcelableArrayList 上显示错误我想获取数组列表并将其存储到该类的本地数组列表中。
如何使用我的自定义对象获取数组列表?
最佳答案
您传递的第一个参数是错误的。 检查定义:
putParcelableArrayList(String key, ArrayList<? extends Parcelable> value)
在您的 fragment 中定义键如下:
public static final String KEY;
要获取数组列表作为 fragment 中的局部变量,请使用以下代码:
@Override
public void onStart() {
super.onStart();
Bundle arguments = getArguments();
ArrayList<customers> customer_list = arguments.getParcelable(KEY);
}
关于android - 如何使用实例将自定义对象数组列表从 Activity 传递到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41376246/