2013-02-22 132 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

相关问题