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];
}
};
}
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];
}
};
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.