Java 使用ArrayList<;字符串>;使用扩展Parcelable的类
我在使用实现Parcelable的类读取字符串的ArrayList时遇到问题 我想向一个片段发送3个字符串数组列表。另一件我不明白的事情是我如何使用这个类将数据发送到一个片段(我在其他地方读到,你可以使用你自己的parcelable类来做这件事,但我不知道确切的方法) 这是相关代码,我会在我认为需要帮助的地方发表评论Java 使用ArrayList<;字符串>;使用扩展Parcelable的类,java,android,arraylist,parcelable,parcel,Java,Android,Arraylist,Parcelable,Parcel,我在使用实现Parcelable的类读取字符串的ArrayList时遇到问题 我想向一个片段发送3个字符串数组列表。另一件我不明白的事情是我如何使用这个类将数据发送到一个片段(我在其他地方读到,你可以使用你自己的parcelable类来做这件事,但我不知道确切的方法) 这是相关代码,我会在我认为需要帮助的地方发表评论 package com.tsjd.HotMeals; import java.util.ArrayList; import android.os.Parcel; import
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];
}
};
}
package com.tsjd.hotfeens;
导入java.util.ArrayList;
导入android.os.packet;
导入android.os.Parcelable;
公共类RecipeListViewParcer实现了Parcelable{
私人ArrayList头衔;
私有数组列表描述;
私有数组列表图像;
私人RecipeListViewParcer(包裹输入){
titles=in.readArrayList(String.class.getClassLoader());//此处需要帮助
descriptions=in.readArrayList(String.class.getClassLoader());//此处需要帮助
images=in.readArrayList(String.class.getClassLoader());//此处需要帮助
}
公共int描述内容(){
返回0;
}
公共无效写入包(包出,内部标志){
out.writeList(标题);//此处需要帮助
out.writeList(描述);//此处需要帮助
out.writeList(图像);//此处需要帮助
}
公共静态最终包裹。创建者
=新的Parcelable.Creator(){
公共RecipeListViewParcer createFromParcel(中的地块){
返回新的RecipeListViewParser(在中);
}
公共RecipeListViewParcer[]新数组(整数大小){
返回新的RecipeListViewParcer[大小];
}
};
}
我已经更改了您的类,添加了一个构造函数来初始化字段,并刚刚重命名
RecipeListViewParcel到RecipeListViewParcel:)
package com.tsjd.hotfeens;
导入java.util.ArrayList;
导入android.os.packet;
导入android.os.Parcelable;
公共类RecipeListViewParcel实现了Parcelable{
私人ArrayList头衔;
私有数组列表描述;
私有数组列表图像;
公共RecipeListViewParcel(ArrayList标题、ArrayList说明、ArrayList图像){
this.titles=标题;
this.descriptions=描述;
这个。图像=图像;
}
//方便的构造函数读取包裹
私人RecipeListViewParcel(地块位于){
titles=in.readArrayList(String.class.getClassLoader());//此处需要帮助
descriptions=in.readArrayList(String.class.getClassLoader());//此处需要帮助
images=in.readArrayList(String.class.getClassLoader());//此处需要帮助
}
公共int描述内容(){
返回0;
}
//此方法执行写入包裹并在内部调用的实际任务
//您需要为每个子类重写此方法
@凌驾
公共无效写入包(包出,内部标志){
out.writeList(标题);//此处需要帮助
out.writeList(描述);//此处需要帮助
out.writeList(图像);//此处需要帮助
}
公共静态最终包裹。创建者
=新的Parcelable.Creator(){
公共RecipeListViewParcel createFromParcel(中的地块){
返回新的RecipeListViewParcel(in);
}
公共RecipeListViewParcel[]新数组(整数大小){
返回新的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];
}
};
}