2013-02-22 164 views
4

我有我的活動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); 
    } 
+0

如果你只是想通過活動,爲什麼它序列之間的自定義對象...只是通過它捆綁。請參閱下面的解決方案 – drulabs 2013-02-22 07:28:11

回答

3

那是因爲你的machinePossession is a List of <Machine>

and 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

+0

我覺得我還是不太對勁。 所以readList想讀取一個列表,但相反,它會告訴我錯誤? 所以真正的問題是我的void writeToParcel沒有正確寫入Parcelobject中的List來傳遞? – Frank 2013-02-22 07:10:52

+0

檢查編輯答案。 – 2013-02-22 07:24:04

+0

你能解釋一下你的最後一句更詳細嗎? – Frank 2013-02-22 08:51:26

0

有一個非常簡單的解決方案。如果你的對象屬於實現parcelable或serializable的類。你可以這樣做:

由於你的工人類實現了parcelable。

從活動1

Bundle bundle = new Bundle(); 
bundle.putParcelable("worker", workerObject); 
bundle.putParcelableArray("workerArray", workerArrayObject); 
bundle.putParcelableArrayList("workerArrayList", workerArrayList); 
Intent i = new Intent(this, Activity2.class); 
i.putExtras(bundle); 
startActivity(i); 

內活性2的onCreate()

Bundle bundle = getIntent().getExtras(); 
Worker workerObject = bundle.getParceable("worker"); 
Worker[] workerArray = bundle.getParceableArray("workerArray"); 
ArrayList<Worker> workerArrayList = bundle.getParceableArrayList("workerArrayList"); 

手柄類型轉換,你是好去...

希望它可以幫助

+0

所以我試過你的解決方案。我得到它的工作,但沒有在workerobject列表。感謝那。我沒有包括: bundle.putParcelableArray(「workerArray」,workerArrayObject); bundle.putParcelableArrayList(「workerArrayList」,workerArrayList); 我不明白爲什麼它在那裏。這僅僅是我能通過的其他例子嗎? 因爲列表在我的workerobject中。 – Frank 2013-02-22 08:47:53

+0

我只是想表明您可以將所有三種類型(workerobject,workerObjectArray和workerObjectArrayList)從一個活動傳遞到另一個活動。如果你只是想使用其中的一個,那麼刪除其他bundle.put(...)。獲得workerObject後,使用它和所需的列表 – drulabs 2013-02-23 16:38:26

相關問題