Nic*_*vil 5 android parcelable
我有MyClass实现Parcelable.并有2个片段.在MainActivity(onCreate)中我有代码:
ArrayList<MyClass> data = new ArrayList<MyClass>();
............
Bundle extras1 = new Bundle();
extras1.putParcelableArrayList("arraylist", data);
Tab1Fragment fg = new Tab1Fragment();
fg.setArguments(extras1);
Run Code Online (Sandbox Code Playgroud)
在Fragment(onCreateView)中:
Bundle extras = getArguments();
ListView list = (ListView) content.findViewById(R.id.lvMain);
if (extras != null) {
data = extras.getParcelableArrayList("arraylist");
list.setAdapter(new MyAdapter(getActivity(), data));
}
Run Code Online (Sandbox Code Playgroud)
但是额外的东西总是空的.为什么?:)
您提供的代码看起来不错。我怀疑问题出在你如何实现 MyClass 上。Parcelable 的典型实现是这样的(取自 google)
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8777 次 |
| 最近记录: |