Reputation: 2055
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);
}
Upvotes: 5
Views: 6932
Reputation: 5428
I solved a similar question to pass a Parcelable with List field and It worked well for me, Here is the example(Give a thumb to me If it helps you):
/**
* 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);
}
}
Upvotes: 0
Reputation: 3121
There is a very simple solution for this. If your Object belongs to a class implementing parcelable or serializable. You can do it like this:
Since your worker class implements parcelable.
From 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's onCreate()
Bundle bundle = getIntent().getExtras();
Worker workerObject = bundle.getParceable("worker");
Worker[] workerArray = bundle.getParceableArray("workerArray");
ArrayList<Worker> workerArrayList = bundle.getParceableArrayList("workerArrayList");
handle typeCasting and you are good to go...
Hope it helps
Upvotes: 0
Reputation: 24012
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
Upvotes: 3