问题描述
使用Retrofit使用json并使用非同步回调后,我无法将ArrayList从MainActivity传递给片段.
After consuming a json with Retrofit and using unsynchronous callback I can't pass An ArrayList from The MainActivity to a fragment .
MainActivity中的代码:
Code from MainActivity:
lFragmentManager = getFragmentManager();
lFragment = lFragmentManager.findFragmentById(R.id.frame_container);
lFragment = new Fragment_Categories();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("list_categories", Categories_.getCategories());
//for(int a = 0 ; a < Categories_.getCategories().size(); a++)
// Log.d("billy",Categories_.getCategories().get(a).getTitle());
lFragment.setArguments(bundle);
lFragmentManager.beginTransaction().replace(R.id.frame_container ,lFragment ).commit();
请注意,在日志内记录"注释确实会打印列表的上下文,因此我将片段中的ArrayList
与此代码包含在onCreateView
中:
Note that the Log inside comments does print the context of the list, so I take the ArrayList
in the fragment with this code inside onCreateView
:
if(savedInstanceState != null)
categories = savedInstanceState.getParcelableArrayList("list_categories");
/*
* initialize the Recycler view
*/
mRecycler = (RecyclerView)view.findViewById(R.id.categories_list);
mAdapter = new AdapterCategories(categories,getActivity());
mRecycler.setAdapter(mAdapter);
这是我的班级类别:
public class Categories implements Parcelable{
private ArrayList<NavDrawerItem> categories;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(categories);
}
public static final Parcelable.Creator<Categories> CREATOR = new Parcelable.Creator<Categories>() {
public Categories createFromParcel(Parcel in) {
return new Categories(in);
}
public Categories[] newArray(int size){
return new Categories[size];
}
};
private Categories(Parcel in){
categories = in.createTypedArrayList(NavDrawerItem.CREATOR);
}
public ArrayList<NavDrawerItem> getCategories() {
return categories;
}
}
这是NavDrawerItem类:
And here is the Class NavDrawerItem:
public class NavDrawerItem implements Parcelable {
private String title;
private String description;
private String image;
private String post_count;
private String id;
private String slug;
private int parent;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeString(description);
dest.writeString(image);
dest.writeString(post_count);
dest.writeString(id);
dest.writeString(slug);
dest.writeInt(parent);
}
public static final Parcelable.Creator<NavDrawerItem> CREATOR = new Parcelable.Creator<NavDrawerItem>() {
public NavDrawerItem createFromParcel(Parcel in) {
return new NavDrawerItem(in);
}
public NavDrawerItem[] newArray(int size){
return new NavDrawerItem[size];
}
};
private NavDrawerItem(Parcel in){
title = in.readString();
description = in.readString();
image = in.readString();
post_count = in.readString();
id = in.readString();
slug = in.readString();
parent = in.readInt();
}
问题是在片段内部(试图将列表传递到RecyclerView
的适配器时)或在RecyclerView
的适配器内部,我采取了空异常:
The problem is that inside fragment (when trying to pass the list to the Adapter of the RecyclerView
) or inside the adapter of the RecyclerView
I take a null exception :
谢谢您的帮助!
推荐答案
您不会使用savedInstanceState
来获取参数,因为它们没有作为saveInstanceState()
操作的一部分传递.您想改用getArguments()
.所以试试这个...
You wouldn't use the savedInstanceState
to get your arguments, because they weren't passed as part of a saveInstanceState()
operation. You want to use getArguments()
instead. So try this...
categories = getArguments().getParcelableArrayList("list_categories");
这篇关于使用Parcelable在片段之间传递ArreyList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!