我的 Activity WorkerActivity 包含一些工作人员的列表,我正在尝试将单击的工作人员对象传递给我的 WorkerDetailActivity。我是 java 和 android 的新手,所以我正在寻找可能的解决方案,因为在这里的大多数答案中建议不要序列化并建议可打包,我尝试按照此处建议的方式执行此操作:
How to pass a parcelable object that contains a list of objects?
最后3行代码,in.readList(machinePossession, null);
部分
显示以下错误:
Type mismatch: cannot convert from void to List<Machine>
我不知道如何处理这个问题,很乐意接受任何建议。
我删除了包和所有构造函数、setter 和 getter 以保持发布的代码干净。
import java.util.ArrayList;
import java.util.List;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
public class Worker implements Parcelable{
private String workerID;
private String workerPersonnelNR;
private String workerLastName;
private String workerName;
private String workerCategory;
private String historyName;
private String historyID;
private String historyFrom;
private String historyTo;
private String historyActive;
private String historyDays;
public List<Machine> machinePossession = new ArrayList<Machine>();
public List<Machine> machinePossibility = new ArrayList<Machine>();
public List<Machine> machineHistory = new ArrayList<Machine>();
public String error;
public static class WorkerWrapper{
public List<Worker> workers;
public WorkerWrapper(List<Worker> workers) {
this.workers = workers;
}
}
public Worker(){
//default constructor
}
/*REMOVED CONSTRUCTORS / SETTERS / GETTERS */
//parcel
public void writeToParcel(Parcel dest, int flags) {
Log.v("", "writeToParcel..." + flags);
dest.writeString(workerID);
dest.writeString(workerPersonnelNR);
dest.writeString(workerLastName);
dest.writeString(workerName);
dest.writeString(workerCategory);
dest.writeString(historyName);
dest.writeString(historyID);
dest.writeString(historyFrom);
dest.writeString(historyTo);
dest.writeString(historyActive);
dest.writeString(historyDays);
dest.writeList(machinePossession);
dest.writeList(machinePossibility);
dest.writeList(machineHistory);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Worker createFromParcel(Parcel in) {
return new Worker(in);
}
public Worker[] newArray(int size) {
return new Worker[size];
}
};
@Override
public int describeContents() {
return 0;
}
private Worker(Parcel in) {
workerID = in.readString();
workerPersonnelNR = in.readString();
workerLastName = in.readString();
workerName = in.readString();
workerCategory = in.readString();
historyName = in.readString();
historyID = in.readString();
historyFrom = in.readString();
historyTo = in.readString();
historyActive = in.readString();
historyDays = in.readString();
machinePossession = in.readList(machinePossession, null);
machinePossibility = in.readList(machineHistory, null);
machineHistory = in.readList(machineHistory, null);
}
}
编辑:
感谢@Archie.bpgc
因此,要消除错误,您必须像这样编写代码:
private Worker(Parcel in) {
workerID = in.readString();
....
historyDays = in.readString();
in.readList(machinePossession, null);
in.readList(machineHistory, null);
in.readList(machineHistory, null);
}
最佳答案
那是因为你的 machinePossession is a List of <Machine>
和in.readList(machinePossession, null) returns void
因此,在这一步:
machinePossession = in.readList(machinePossession, null);
你得到
Type mismatch: cannot convert from void to List
同时试图保持 void in a List<Machine>
来自 Docs
public final void readList (List outVal, ClassLoader loader)
Added in API level 1 Read into an existing List object from the parcel at the current dataPosition(), using the given class loader to load any enclosed Parcelables. If it is null, the default class loader is used.
我觉得
in.readList(machinePossession, null);
就够了。不要将其分配给 List<machine>
, 而不是用它来 read in to
关于android - 使用 Parcelable 将包含对象列表的对象传递给另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15018321/