2

我在使用实现 Parcelable 的类读取字符串的 ArrayList 时遇到问题。

我想将 3 个 ArrayLists 的字符串发送到一个片段。我不明白的另一件事是我将如何使用此类将这些数据实际发送到片段(我在其他地方读到您可以使用自己的可打包类来执行此操作,但我不知道具体如何)。

这是相关代码,我会在我认为需要帮助的地方添加注释。

package com.tsjd.HotMeals;

import java.util.ArrayList;

import android.os.Parcel;
import android.os.Parcelable;

public class RecipeListViewParcer implements Parcelable{

    private ArrayList<String> titles;
    private ArrayList<String> descriptions;
    private ArrayList<String> images;

     private RecipeListViewParcer(Parcel in) {
            titles = in.readArrayList(String.class.getClassLoader()); //Need help here
            descriptions = in.readArrayList(String.class.getClassLoader()); //Need help here
            images = in.readArrayList(String.class.getClassLoader()); //Need help here
        }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeList(titles); //Need help here
        out.writeList(descriptions); //Need help here
        out.writeList(images); //Need help here
    }

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

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



}
4

1 回答 1

0

我已经改变了你的类,增加了一个构造函数来初始化字段,只是将 RecipeListViewParcer 重命名为 RecipeListViewParcel :)

package com.tsjd.HotMeals;

import java.util.ArrayList;

import android.os.Parcel;
import android.os.Parcelable;

public class RecipeListViewParcel implements Parcelable{

    private ArrayList<String> titles;
    private ArrayList<String> descriptions;
    private ArrayList<String> images;

    public RecipeListViewParcel(ArrayList<String> titles, ArrayList<String> descriptions, ArrayList<String> images) {
        this.titles = titles;
        this.descriptions = descriptions;
        this.images = images;
    }

    // Convenient constructor to read from Parcel
    private RecipeListViewParcel(Parcel in) {
        titles = in.readArrayList(String.class.getClassLoader()); //Need help here
        descriptions = in.readArrayList(String.class.getClassLoader()); //Need help here
        images = in.readArrayList(String.class.getClassLoader()); //Need help here
    }

    public int describeContents() {
        return 0;
    }

    // This method does actual job to write into the Parcel and called internally
    // You need to override this method for each child class
    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeList(titles); //Need help here
        out.writeList(descriptions); //Need help here
        out.writeList(images); //Need help here
    }

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

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

}
于 2014-06-06T09:44:31.423 回答