2017-09-07 16 views
0

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; 
} 

答えて

1

あなたが作成したオブジェクトを使用していることを確認してください。

これは動作するはずです。

IngredientListFragment ingredientListFragment = IngredientListFragment.newInstance(mRecipe.getIngredients()); 

getSupportFragmentManager() 
     .beginTransaction() 
     .add(R.id.first_fragment_container, ingredientListFragment) 
     .commit(); 
+0

これは感謝、児童犯罪のエラーでした。 – Werner

+0

この答えを正しいものとしてマークするとよいでしょう。 @werner – rajatgoyal715

1
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()); 
+0

OPが求めていたコードを提案できますか? – Aydin4ik

関連する問題