0

I know downcasting isn't allowed in Java but is there a way to cast an array list of parcelable objects into an array list of cholesterolMonitors which implements parcelable?

In one of my abstract classes I have:

private ArrayList<Parcelable> objectList;
public ArrayList<Parcelable> getList() {
    return objectList;
}

Then a concrete class which extends from this class has:

    private ArrayList<CholesterolMonitor> monitor_list;
    monitor_list=getList();//Retuns error here because for incompatible types

My cholesterol monitor class is:

public static final Creator<CholesterolMonitor> CREATOR = new 

Creator<CholesterolMonitor>() {
        @Override
        public CholesterolMonitor createFromParcel(Parcel in) {
            return new CholesterolMonitor(in);
        }

        @Override
        public CholesterolMonitor[] newArray(int size) {
            return new CholesterolMonitor[size];
        }
    };
@Override
public void writeToParcel(Parcel dest, int flags) {
}
Sook Lim
  • 541
  • 6
  • 28
  • 1
    I think that instead of `ArrayList` you need `ArrayList extends Parcelable>`. See https://stackoverflow.com/a/10712625/115145 – CommonsWare May 16 '19 at 22:53
  • I tried that but when I call getList and assign it to monitor_list I still get the error for incompatible types?EDIT:Fixed I can now cast it to cholesterol monitor. – Sook Lim May 17 '19 at 00:52

0 Answers0