2014-06-06 54 views
1

類我遇到了一個實現Parcelable的類讀取字符串ArrayList的麻煩。使用一個ArrayList <String>與擴展Parcelable

我想發送3個字符串ArrayLists到一個片段。我不明白的另一件事是我如何使用這個類來實際地將這些數據發送到Fragment(我讀過其他地方,你可以使用你自己的parcelable類來做到這一點,但我不知道如何)。

這裏是相關的代碼,我會在我認爲我需要幫助的地方發表評論。

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



} 

回答

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

}