Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass an object containing list of objects to another activity using Parcelable

I have my activity WorkerActivity with a list of some worker and I'm trying to pass the worker object clicked to my WorkerDetailActivity. I'm relative new to java and android, so i was searching for possible solutions and since in most answers here it was advised against serialized and advised for parcelable, I tried to do this like proposed here:
How to pass a parcelable object that contains a list of objects?

The last 3 lines of code, the part in.readList(machinePossession, null); shows me following error:

Type mismatch: cannot convert from void to List<Machine>

I don't know how to deal with this and would gladly accept any suggestions.


I removed package and all the constructors, setters and getters to keep the posted 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 to @Archie.bpgc
So to get rid of the error you have to code it like this:

private Worker(Parcel in) {
    workerID = in.readString();   
    ....
    historyDays = in.readString(); 
    in.readList(machinePossession, null);
    in.readList(machineHistory, null);
    in.readList(machineHistory, null);
    }
like image 794
Frank Avatar asked Oct 22 '22 16:10

Frank


1 Answers

Thats because your machinePossession is a List of <Machine>

and in.readList(machinePossession, null) returns void

hence, at this step:

machinePossession = in.readList(machinePossession, null);

you get

Type mismatch: cannot convert from void to List

while trying to keep void in a List<Machine>

From 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.

So, i think

in.readList(machinePossession, null);

will suffice. Don't assign it to the List<machine>, instead use it to read in to

like image 146
Archie.bpgc Avatar answered Oct 24 '22 09:10

Archie.bpgc