ArrayList cannot be added to a custom class that extends ArrayList

I have a class mentioned below:

public class JsonHistoryList extends ArrayList<JsonHistory> implements Serializable{}

      

I want to pass it using

timerService.putExtra(TimerService.ACTIVITY_LIST_ARG, activities);

      

But after I receive it (path is below)

JsonHistoryList temp = (JsonHistoryList) intent.getSerializableExtra(TimerService.ACTIVITY_LIST_ARG);

      

in my service this gives me an exception:

Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.epstim.captime.jsonmodel.JsonHistoryList

      

I don't understand why Java cannot handle this operation.

I changed my code on the service:

ArrayList<JsonHistory> temp = (ArrayList<JsonHistory>) intent.getSerializableExtra(TimerService.ACTIVITY_LIST_ARG);
activities.addAll(temp);

      

And it worked.

+4


source to share


5 answers


Internally, android puts each extra in a dedicated card and doesn't record exactly how you want it to be split.

At some point android will align your paddings into the premise, and it will do so by checking each object type (since, as I said, it doesn't remember how you want it).

The batch supports both writeList and writeSerializable, and your JsonHistoryList is also at the same time (serializable list and itself serializable)

So, android parking looks like this:



for (Object object : extras) {
   //... check for other types here
   } else if (object instanceof List) {
     parcel.writeList(object); // This what happens in your case and will be unparcelled into arraylist
   } else if (object instanceof Serializable) {
     parcel.writeSerializable(object); // What you really want, but percelling never will get here
   }
}

      

If you want to keep the list, you need to create a class that will be serializable and will not propagate the arraylist, but will contain it internally.

public class SuperJsonHistory implements Serializable {
  private JsonHistoryList yourList;
  ...
}

      

So composition over inheritance in case you want to keep the type

+9


source


This happens if it intent.getSerializableExtra(TimerService.ACTIVITY_LIST_ARG);

returns a class object ArrayList<JsonHistory>

, not an object of type JSONHistoryList

.

We cannot forcibly reset the parent object.

Consider your example, say



Public class JsonHistoryList extends ArrayList implements Serializable {public int someField; // can do anything}

For simplicity's sake, if your getSerializableExtra();

returns, new ArrayList<JsonHistory>()

when you try to dump that value to JSONHistoryList

, it cannot be executed since the values someField

cannot be determined

+1


source


we cannot include a superclass object in a subclass object.

Entry is possible only if the object to be executed conveys the IS-A relation.

you can take a look at this link. It will clear all doubts explicit casting from superclass to subclass

0


source


So if JsonHistoryList extends ArrayList

, then a JsonHistoryList

is ArrayList

, but a is ArrayList

not necessarily a JsonHistoryList

. You expected your own subclass, but that is not what you returned.

0


source


I faced the same problem as you, I just added a wrapper class that implements Serializable:

public class JsonHistoryListWrapper implement Serializable {
    JsonHistoryList jsonHistoryList;

    private JsonHistoryListWrapper(JsonHistoryList jsonHistoryList) {
        this.jsonHistoryList = jsonHistoryList;
    }

    public static JsonHistoryListWrapper wrapper(JsonHistoryList jsonHistoryList) {
        return new JsonHistoryListWrapper(jsonHistoryList);
    }
}

      

0


source







All Articles