【发布时间】:2018-07-27 19:54:44
【问题描述】:
下面是活动代码的快照。我想将列表发送到单独的片段。第一个工作正常,但是在 stepsFragment.setArguments(stepsBundle);无法识别 setArguments。谢谢。
//
// Send the ingredients array list in Parcelable to the Ingredients Fragment
//
private void sendArrayToIngredientsFragment() {
//Pack Data in a bundle(call the bundle "ingredientsBundle" to differentiate it from the "stepsBundle"
Bundle ingredientsBundle = new Bundle();
ingredientsBundle.putParcelable("Recipes", recipes);
//Pass Over the bundle to the Ingredients Fragment
IngredientsFragment ingredientsFragment = new IngredientsFragment();
ingredientsFragment.setArguments(ingredientsBundle);
getSupportFragmentManager().beginTransaction().replace(R.id.ingredients_fragment_container, ingredientsFragment).commit();
}
/*
Send the steps array list in Parcelable to the Steps Fragment
*/
private void sendArrayToStepsFragment() {
//Pack Data in a bundle(call the bundle "stepsBundle" to differentiate it from the "ingredientsBundle"
Bundle stepsBundle = new Bundle();
stepsBundle.putParcelable("Recipes", recipes);
//Pass Over the bundle to the Steps Fragment
StepsFragment stepsFragment = new StepsFragment();
stepsFragment.setArguments(stepsBundle);
getSupportFragmentManager().beginTransaction().replace(R.id.steps_fragment_container, stepsFragment).commit();
}
}
【问题讨论】:
-
stepsFragment.setArguments有什么问题?是的,您可以将数据传递给您的片段。在片段类本身中创建一个静态方法的推荐方法是 public static Fragment newInstance() { fragment = ;捆绑捆绑 = 新捆绑(); // 添加数据到 bundle fragment.setArguments(bundle);返回片段; } -
消息是:setArguments 无法识别。
-
如果你还没有找到答案,把
StepsFragment的代码贴出来可能会有所帮助 -
谢谢。我已经想通了。
标签: android android-fragments bundle parcelable