Parcelableをバンドルとして自分のActivityからMy Fragmentに拡張するカスタムPOJOを渡そうとしています。ポピュレートされたバンドルオブジェクトは、正常に私のFragmentのnewInstanceメソッドに渡されます。しかし、私のPOJOクラスのこのArrayListを回復しようとすると、私のFragmentのonCreateメソッドで何とか落としてしまいます。Android:ParcelableをActivityからFragmentに束縛するカスタムPOJOのArrayListを渡す
//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
}
私は私の断片の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;
}
これは感謝、児童犯罪のエラーでした。 – Werner
この答えを正しいものとしてマークするとよいでしょう。 @werner – rajatgoyal715