5

我有我的活动 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);
    }
4

3 回答 3

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>

来自文档

public final void readList (List outVal, ClassLoader loader)

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

所以我认为

in.readList(machinePossession, null);

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

于 2013-02-22T06:48:44.150 回答
0

有一个非常简单的解决方案。如果您的 Object 属于实现 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);

在 Activity2 的 onCreate() 内部

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

处理类型转换,你很高兴......

希望能帮助到你

于 2013-02-22T06:52:25.187 回答
0

我解决了一个类似的问题来传递一个带有列表字段的 Parcelable,它对我来说效果很好,这是示例(如果对您有帮助,请给我一个大拇指):</p>

 /**
  * 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);
   }

 }
于 2018-06-26T11:10:36.693 回答