我有我的活動WorkerActivity與一些工人的列表,我試圖通過點擊我的WorkerDetailActivity的工人對象。我是相對新的java和android,所以我正在尋找可能的解決方案,因爲在這裏大多數答案建議反對序列化和建議爲parcelable,我試圖做這樣的提議在這裏:
How to pass a parcelable object that contains a list of objects?將包含對象列表的對象傳遞給使用Parcelable的其他活動
最後3行代碼,部分in.readList(machinePossession, null);
顯示我以下錯誤:
Type mismatch: cannot convert from void to List<Machine>
我不知道如何處理這一點,並非常樂意接受任何建議。
我刪除了包和所有的構造函數,setters和getters來保持發佈代碼的清潔。
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);
}
如果你只是想通過活動,爲什麼它序列之間的自定義對象...只是通過它捆綁。請參閱下面的解決方案 – drulabs 2013-02-22 07:28:11