Android – use Parcelable to pass an object containing a list of objects to another activity

My activity workeractivity contains a list of workers. I'm trying to pass the clicked worker object to my workerdetaileactivity. I'm unfamiliar with Java and Android, so I've been looking for possible solutions. In most answers, it is recommended not to serialize and it is recommended to use Parcelable, I try to do the same as proposed here: how to pass a separable object that contains a list of objects?

The last three lines of code, part in. Readlist (machinepossession, null); Tell me the following error:

I don't know how to deal with this problem and am willing to accept any suggestions

I removed the package and all constructors, setters and getters to keep the published code clean

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

Edit:

Thanks @ archie.bpgc, so to get rid of errors, you must code like this:

private Worker(Parcel in) {
    workerID = in.readString();   
    ....
    historyDays = in.readString(); 
    in.readList(machinePossession, null);
    in.readList(machineHistory, null);
    in.readList(machineHistory, null);
    }

resolvent:

That's because your machinepossession is a list of < machine >

And in. Readlist (machinepossession, null) return void

Therefore, in this step:

machinePossession = in.readList(machinePossession, null);

You get

Attempt to keep invalid in list < machine >

From docs

So I think

in.readList(machinePossession, null);

That's enough. Instead of assigning it to list < machine >, use it to read in

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>