简体   繁体   English

如何将带有ArrayList的捆绑包从片段传递到活动

[英]How to pass bundle with ArrayList from Fragment to Activity

I have a ListFragment that implements a click listener to pass clicked object to IngredientActivity. 我有一个ListFragment,它实现一个单击侦听器,以将单击的对象传递给IngredientActivity。 When I retrieve the object in the IngredientActivity, the objects's variables are null. 当我在IngredientActivityActivity中检索对象时,对象的变量为null。

public class ListFragment extends Fragment implements ListRecipeAdapter.ListItemClickListener {
....
@Override
    public void onListItemClick(Recipe selectedRecipe) {
        Bundle bundle = new Bundle();
        ArrayList<Recipe> selectedRecipeArray = new ArrayList<>();
        selectedRecipeArray.add(selectedRecipe);
        bundle.putParcelableArrayList("recipe",selectedRecipeArray);
        Intent intent = new Intent(getActivity(), IngredientActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}

I retrieve the bundle and extract the Arraylist of object as: 我检索捆绑并将对象的Arraylist提取为:

public class IngredientActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ....
        Bundle bundle = getIntent().getExtras();
        ArrayList<Recipe> recipes = bundle.getParcelableArrayList("recipe");
        recipe = recipes.get(0);

    }
}

The listener is interface: 侦听器是接口:

public interface ListItemClickListener {
        void onListItemClick(Recipe recipeId);
    }

Recipe is defined as a Parcelable: 配方定义为可包裹:

public class Recipe implements Parcelable {
...
}

When I print this in logcat: 当我在logcat中打印时:

Recipe in Fragment: 片段中的食谱:

com.example.android.bakingapp.Models.Recipe@5d8b71e

ArrayList in Fragment: 片段中的ArrayList:

[com.example.android.bakingapp.Models.Recipe@5d8b71e]

Bundle in Fragment: 打包成片段:

Bundle[{selected_recipe=[com.example.android.bakingapp.Models.Recipe@5d8b71e]}]

The retrieved object looks different: 检索到的对象看起来有所不同:

Bundle in Activity: 捆绑活动:

Bundle[mParcelledData.dataSize=6784]

ArrayList in Activity: 活动中的ArrayList:

[com.example.android.bakingapp.Models.Recipe@1abf1e8]

Recipe in Activity: 活动中的食谱:

com.example.android.bakingapp.Models.Recipe@1abf1e8

Did you implement the Parcelable object correctly? 您是否正确实现了Parcelable对象?

After parceled, you can have different address object because object are recreated with that data. 打包后,您可以拥有其他地址对象,因为将使用该数据重新创建对象。

Check out the Parcelable implementation of the Recipe object. 签出Recipe对象的Parcelable实现。

It looks like you have issue in your parcelable object. 看起来您的可包裹对象有问题。 Make sure all fields are serialized right. 确保所有字段都正确序列化。

Another option - your parcelable object is too large. 另一种选择-您的可包裹对象太大。 There is size limitation on Parcelable that is attached to intent - something about 1 mb. 附加到Intent的Parcelable有大小限制-大约1 mb。

I have a ListFragment that implements a click listener to pass clicked object to IngredientActivity. 我有一个ListFragment,它实现一个单击侦听器,以将单击的对象传递给IngredientActivity。 When I retrieve the object in the IngredientActivity, the objects' variables are null. 当我在IngredientActivity中检索对象时,对象的变量为null。

I would like you to rethink the structure, what you are doing is passing the ArrayList object via Bundle from Fragment relevant Activity , which is the not the proper way to go. 我希望您重新考虑结构,您正在做的是通过Fragment相关Activity通过Bundle传递ArrayList对象,这不是正确的方法。

Solution: You may want to use interface which would be going to pass the clicked object via callback method to activity(Here I am assuming you will need to set the interface once fragment is created) 解决方案:您可能想要使用将通过回调方法将单击的对象传递给活动的接口(这里我假设一旦创建片段,您将需要设置接口)

Make like 使像

        public class ListFragment extends Fragment {
        ....

        private ListItemClickListener mListener;

        public void setListClickListner(ListItemClickListener listener){
        this.mListener=listener;
        }

/**Note : you will need to call      
mListener.onListItemClick(selectedRecipe);
in the relavent click event of ListView/RecyclerView with instance of 
receipe. */

}

and in Activity: 在活动中:

public class IngredientActivity extends AppCompatActivity  implements ListRecipeAdapter.ListItemClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ....
        Bundle bundle = getIntent().getExtras();
        ArrayList<Recipe> recipes = bundle.getParcelableArrayList("recipe");
        recipe = recipes.get(0);

    }

    // Callback method
   @Override
    public void onListItemClick(Recipe selectedRecipe) {
        // Whatever thing you want to do goes here...
        mListener.onListItemClick(selectedRecipe);
    }

}

I hope it makes sense. 我希望这是有道理的。

You can use Android Parcelable or Java Serializable interface. 您可以使用Android Parcelable或Java Serializable接口。 Both will work. 两者都会起作用。 With performance level, parcelable is faster than serializable. 在性能方面,可打包比可序列化要快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM