[英]Android: Pass an ArrayList of custom POJO which extends Parcelable as a Bundle from Activity to Fragment
I'm trying to pass a custom POJO which extends Parcelable as a Bundle from my Activity to my Fragment. 我正在尝试传递自定义POJO,该POJO将Parcelable作为Bundle从我的Activity扩展到我的Fragment。 The populated bundle object gets successfully passed to my Fragment's newInstance method.
填充的bundle对象已成功传递到Fragment的newInstance方法。 However in my Fragment's onCreate method when I try to recover this ArrayList of my POJO class it somehow gets dropped.
但是,当我尝试恢复POJO类的ArrayList时,在Fragment的onCreate方法中,它以某种方式被删除。
//Activity
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
mRecipe = getIntent().getParcelableExtra("com.wernerraubenheimer.bakingapp.data.RecipeModel");
//This works mRecipe is successfully populated with my custom ArrayList<IngredientModel>
//My class IngredientModel extends Parcelable
IngredientListFragment ingredientListFragment = IngredientListFragment.newInstance(mRecipe.getIngredients());
//Still good so far....
getSupportFragmentManager()
.beginTransaction()
.add(R.id.first_fragment_container, new IngredientListFragment())
.commit();
}
//Fragment
public static IngredientListFragment newInstance(ArrayList<IngredientModel> ingredients) {
IngredientListFragment ingredientListFragment = new IngredientListFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("ingredients", ingredients);
ingredientListFragment.setArguments(bundle);
return ingredientListFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//This is where the ball gets dropped, mIngredientList which I initialized when I declared it just after
//class declaration: private ArrayList<IngredientModel> mIngredientList = new ArrayList<>();
//is now empty??
//I have also placed the call to super.onCreate after the statement below
mIngredientList = getArguments().getParcelableArrayList("ingredients");
//have also used savedInstanceState
}
I want to use the ArrayList in a RecyclerView in the onCreateView of my Fragment: 我想在Fragment的onCreateView的RecyclerView中使用ArrayList:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_ingredient_list, container, false);
rvIngredientList = (RecyclerView)rootView.findViewById(R.id.rv_ingredient_list);
ingredientLayoutManager = new LinearLayoutManager(getActivity());
ingredientLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rvIngredientList.setLayoutManager(ingredientLayoutManager);
IngredientListAdapter ingredientListAdapter = new IngredientListAdapter(mIngredientList);
rvIngredientList.setAdapter(ingredientListAdapter);
return rootView;
}
getSupportFragmentManager()
.beginTransaction()
.add(R.id.first_fragment_container, new
IngredientListFragment())
.commit();
in this method you are providing new instance of fragment, rather than the one which you created using
IngredientListFragment ingredientListFragment = IngredientListFragment.newInstance(mRecipe.getIngredients());
Please make sure you are using the object you created. 请确保您使用的是创建的对象。
This should work. 这应该工作。
IngredientListFragment ingredientListFragment = IngredientListFragment.newInstance(mRecipe.getIngredients());
getSupportFragmentManager()
.beginTransaction()
.add(R.id.first_fragment_container, ingredientListFragment)
.commit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.