android - 使用 Parcelable 将包含对象列表的对象传递给另一个 Activity

标签 android object android-intent parcelable

我的 Activity WorkerActivity 包含一些工作人员的列表,我正在尝试将单击的工作人员对象传递给我的 WorkerDetailActivity。我是 java 和 android 的新手,所以我正在寻找可能的解决方案,因为在这里的大多数答案中建议不要序列化并建议可打包,我尝试按照此处建议的方式执行此操作:
How to pass a parcelable object that contains a list of objects?

最后3行代码,in.readList(machinePossession, null);部分 显示以下错误:

Type mismatch: cannot convert from void to 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);
    }

最佳答案

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

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

关于android - 使用 Parcelable 将包含对象列表的对象传递给另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15018321/

相关文章:

android - 如何克服这个错误 android.permission.MODIFY_PHONE_STATE.?

android "no applications can perform this action"使用 "send_multiple",sendto 工作正常

java - 知道 Android 中点击了哪个按钮

java - org.json.JSONException : Value All fields must be completed at msg of type java. lang.String 无法转换为 JSONArray

javascript - 从对象数组中添加新的键和值集

Javascript:连接多个包含路径的数组并以特殊方式转换为对象

JavaScript 从一个值执行 Object.entries()

android - 找不到处理 Intent 的 Activity { act=android.intent.action.PICK dat=content ://media/external/images/media }

android - 从后台应用程序启动 Activity

Android Studio,模拟器打不开