使用 ArrayList<String>使用扩展 Parcelable 的类 [英] Using an ArrayList&lt;String&gt; with a class that extends Parcelable

查看:46
本文介绍了使用 ArrayList<String>使用扩展 Parcelable 的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读取带有实现 Parcelable 的类的字符串 ArrayList 时遇到问题.

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

我想将 3 个字符串的 ArrayLists 发送到 Fragment.我不明白的另一件事是我如何使用这个类将这些数据实际发送到 Fragment (我在其他地方读到你可以使用你自己的 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];
        }
    };



}

推荐答案

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

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

}

这篇关于使用 ArrayList<String>使用扩展 Parcelable 的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆