1

我有parcelable类类别

public static class Category implements Parcelable {
    public String name;
    public int id;
    public ArrayList<Category> childrenCategory;

    public Category(Parcel in) {
        super();
        readFromParcel(in);
    }

    public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() {
        public Category createFromParcel(Parcel in) {
            return new Category(in);
        }

        public Category[] newArray(int size) {
            return new Category[size];
        }

    };

    public void readFromParcel(Parcel in) {
        this.name = in.readString();
        this.id = in.readInt();
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(id);
    }

    @Override
    public int describeContents() {
        return 0;
    }
}

我想以这种方式通过 Intent

public void onClick(View v) {
            ArrayList<Categories> categories = new ArrayList<Categories>();
            Intent intent = new Intent(getApplicationContext(),
                    MainActivity.class);

            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList("arrayListName", categories);
            inten.putExtra("bundleName", bundle);
            startActivity(intent);
        }

并用这段代码阅读

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
ArrayList<Category> categories = b.getParcelableArrayList("arrayListName");

我缺少 readFromParcel 和 writeToParcel 方法的代码。

任何人都可以提出一些建议。

4

0 回答 0