我有我的活动 WorkerActivity 和一些工人的列表,我试图将点击的工人对象传递给我的 WorkerDetailActivity。我对 java 和 android 比较陌生,所以我正在寻找可能的解决方案,因为在大多数答案中,建议不要序列化并建议使用 parcelable,我尝试像这里建议的那样执行此操作:
How to pass a parcelable object that contains对象列表?
最后 3 行代码,该部分in.readList(machinePossession, null);
向我显示以下错误:
类型不匹配:无法从 void 转换为 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);
}