0

Code structure

public interface ListItem
public abstract class Device implements ListItem
public class DeviceList extends ArrayList<Device>
public class ListAdapter extends BaseAdapter{
...
    public ArrayList<ListItem> items;
...
    }

I want to pass DeviceList to ListAdapter, but can't cast DeviceList to ArrayList< ListItem>. Why is that?

If Device implements ListItem, and DeviceList is an array list of Device, shouldn't I be able to cast it?

I try it like this:

ArrayList<ListItem> list2 = (ArrayList<ListItem>) device_list;
Invader Zim
  • 796
  • 2
  • 14
  • 39

1 Answers1

4

That's a common misunderstanding. Although Device is a ListItem, a List of Devices is not a List of ListItems. Btw, this already had been answered, see for example How to cast List<Object> to List<MyClass> and How do you cast a List of supertypes to a List of subtypes?

Headcracker
  • 522
  • 4
  • 19