使用Parcelable将包含对象列表的对象传递给另一个活动

时间:2013-02-22 06:36:55

标签: android object android-intent parcelable

我的活动WorkerActivity包含一些worker的列表,我正在尝试将单击的worker对象传递给我的WorkerDetailActivity。我对java和android比较陌生,所以我一直在寻找可能的解决方案,因为在大多数答案中,建议不要序列化并建议parcelable,我试着像这里提出的那样做:
{{3} }

最后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);
    }

3 个答案:

答案 0 :(得分:3)

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

in.readList(machinePossession, null) returns void

因此,在这一步:

machinePossession = in.readList(machinePossession, null);

你得到了

  

类型不匹配:无法从void转换为List

尝试保留void in a List<Machine>

来自Docs

  

public final void readList(List outVal,ClassLoader loader)

     

在API级别1中添加从包裹中读取现有的List对象   在当前的dataPosition()中,使用给定的类加载器进行加载   任何封闭的Parcelables。如果为null,则默认类加载器为   使用

所以,我认为

in.readList(machinePossession, null);

就足够了。不要将其分配给List<machine>,而是将其用于read in to

答案 1 :(得分:0)

有一个非常简单的解决方案。如果您的Object属于实现parcelable或serializable的类。你可以这样做:

因为你的worker类实现了parcelable。

来自Activity1

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);

Inside Activity2的onCreate()

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

处理typeCasting,你很高兴...

希望有所帮助

答案 2 :(得分:0)

我解决了一个类似的问题,以通过“带列表的包裹”字段,它对我来说很好用,这是示例(如果有帮助,请给我个大拇指):

 /**
  * Note: You have to keep an order of writing and reading
  * If you have more than one field.
  **/
 public class Test implements Parcelable {

   // Machine also implements Parcelable.
   private List<Machine> machinePossession;

   public static final Creator<Test> CREATOR = new Creator<Test>() {

      @Override
      public Test createFromParcel(Parcel in) {
         return new Test(in);
      }

      @Override
      public DirectStoreProductBean[] newArray(int size) {
         return new DirectStoreProductBean[size];
      }
   };

   protected Test(Parcel in) {

      // This is a must to make sure the machinePossession isn't null.
      // Anyway, you can ignore this line If you can make sure machinePossession isn't null.
      machinePossession = new ArrayList<>();

      // You have to make Machine class implement Parceable and create a CREATOR in it.
      in.readTypedList(machinePossession, Machine.CREATOR);
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
      // It's better to log machinePossession's info here to make suer it contains values.
      dest.writeTypedList(machinePossession);
   }

 }
相关问题