使用一个ArrayList&LT;串GT;与扩展Parcelable类 [英] Using an ArrayList<String> with a class that extends Parcelable
问题描述
我在与阅读字符串的ArrayList使用实现Parcelable类的麻烦。
我想给串3的ArrayList到一个片段。我不明白的一件事是如何我使用这个类实际这个数据发送到一个片段(我看别的地方,你可以使用自己的parcelable类来做到这一点,但我不知道究竟是如何)。
下面是相关code,我会把意见中,我想我需要帮助的地方。
包com.tsjd.HotMeals;进口的java.util.ArrayList;进口android.os.Parcel;
进口android.os.Parcelable;公共类RecipeListViewParcer实现Parcelable { 私人的ArrayList&LT;串GT;标题;
私人的ArrayList&LT;串GT;描述;
私人的ArrayList&LT;串GT;图像; 私人RecipeListViewParcer(包裹中){
标题= in.readArrayList(String.class.getClassLoader()); //需要帮助这里
描述= in.readArrayList(String.class.getClassLoader()); //需要帮助这里
图像= in.readArrayList(String.class.getClassLoader()); //需要帮助这里
} 公众诠释describeContents(){
返回0;
} 公共无效writeToParcel(包裹出来,诠释标志){
out.writeList(职称); //需要帮助这里
out.writeList(描述); //需要帮助这里
out.writeList(图像); //需要帮助这里
} 公共静态最终Parcelable.Creator&LT; RecipeListViewParcer&GT; CREATOR
=新Parcelable.Creator&LT; RecipeListViewParcer&GT;(){
公共RecipeListViewParcer createFromParcel(包裹中){
返回新RecipeListViewParcer(中);
} 公共RecipeListViewParcer [] newArray(INT大小){
返回新RecipeListViewParcer【尺寸】;
}
};}
我已经改变了你的类增加一个构造函数来初始化域和刚刚更名
RecipeListViewParcer到RecipeListViewParcel:)
包com.tsjd.HotMeals;进口的java.util.ArrayList;进口android.os.Parcel;
进口android.os.Parcelable;公共类RecipeListViewParcel实现Parcelable { 私人的ArrayList&LT;串GT;标题;
私人的ArrayList&LT;串GT;描述;
私人的ArrayList&LT;串GT;图像; 公共RecipeListViewParcel(ArrayList的&LT;串GT;标题,ArrayList的&LT;串GT;弦乐&GT;的ArrayList&LT描述,图像){
this.titles =职称;
this.descriptions =描述;
this.images =图像;
} //方便的构造从包裹阅读
私人RecipeListViewParcel(包裹中){
标题= in.readArrayList(String.class.getClassLoader()); //需要帮助这里
描述= in.readArrayList(String.class.getClassLoader()); //需要帮助这里
图像= in.readArrayList(String.class.getClassLoader()); //需要帮助这里
} 公众诠释describeContents(){
返回0;
} //此方法不实际工作写入到地块和内部调用
//你需要覆盖的每个子类这种方法
@覆盖
公共无效writeToParcel(包裹出来,诠释标志){
out.writeList(职称); //需要帮助这里
out.writeList(描述); //需要帮助这里
out.writeList(图像); //需要帮助这里
} 公共静态最终Parcelable.Creator&LT; RecipeListViewParcel&GT; CREATOR
=新Parcelable.Creator&LT; RecipeListViewParcel&GT;(){
公共RecipeListViewParcel createFromParcel(包裹中){
返回新RecipeListViewParcel(中);
} 公共RecipeListViewParcel [] newArray(INT大小){
返回新RecipeListViewParcel【尺寸】;
}
};}
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];
}
};
}
这篇关于使用一个ArrayList&LT;串GT;与扩展Parcelable类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!