简体   繁体   English

Android:传递自定义POJO的ArrayList,该列表将Parcelable作为Bundle捆绑从Ac​​tivity扩展到Fragment

[英]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.

相关问题 如何将带有ArrayList的捆绑包从片段传递到活动 - How to pass bundle with ArrayList from Fragment to Activity 无法将可拆分对象ArrayList传递给片段活动 - Not able to pass parcelable object ArrayList to a fragment activity 可与 arraylist 打包<CustomClass>将值从活动传递给片段 android - Parcelable with arraylist<CustomClass> passing value from activity to fragment android 可与arraylist合并 <string> 将值从活动传递到片段android - Parcelable with arraylist<string> passing value from activity to fragment android 如何将自定义数组列表从片段传递到活动 - How to Pass Custom arraylist from fragment to activity 将 Parcelable arraylist 从 MainActivity 传递给 Fragment - Pass parcelable arraylist from MainActivity to Fragment 如何在Android中将ArrayList从Activity传递到Fragment - How to Pass ArrayList from Activity to Fragment in Android 使用bundle,seListAdapter运行时错误将ArrayList从片段传递到另一个片段(扩展ListFragment) - Pass ArrayList from fragment to another fragment(extends ListFragment) using bundle, seListAdapter runtime error 将可分割对象从片段传递到活动 - Pass an Parcelable Object from an Fragment to an activity 如何将捆绑从一个活动传递到放置在另一个活动中的片段? - how to pass bundle from an activity to a fragment which is placed in another activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM