Using an ArrayList<String> with a class that extends Parcelable

我与影子孤独终老i 提交于 2019-12-08 08:32:47

问题


I'm having trouble with reading an ArrayList of Strings with a class that implements Parcelable.

I want to send 3 ArrayLists of Strings to a Fragment. Another thing I don't understand is how I'd use this class to actually send this data to a Fragment (I read somewhere else that you can use your own parcelable class to do this, but I don't know exactly how).

Here is the relevant code, I'll put comments in the places I think I need help.

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];
        }
    };



}

回答1:


I have changed you class adding one more constructor to initialize fields and just renamed RecipeListViewParcer to 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];
        }
    };

}


来源:https://stackoverflow.com/questions/24077342/using-an-arrayliststring-with-a-class-that-extends-parcelable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!