Search code examples
javaandroidbaseadapter

Android - Pass Multiple List<AbstractType> to BaseAdapter


I have an abstract class AbstractCard which I want to use as my List data to pass to my BaseAdapter. I don't want to pass a List<AbstractCard> though to the BaseAdapter but instead pass a List<Object> that extends AbstractCard.

For example, I have a class Card that extends AbstractCard. In my BaseAdapter I have:

private List<AbstractCard> abstractCards = new ArrayList<AbstractCard>();

public void setData(List<AbstractCard> abstractCards) {
    this.abstractCards = abstractCards;
}

Now, since Card extends AbstractCard, I would think that I could pass a List<Card> to setData() but it obviously gives me errors. How can I make it so I require a type based off of its parent class?


Solution

  • Have you tried with:

    private List<? extends AbstractCard> abstractCards = new ArrayList<AbstractCard>();
    
    public void setData(List<? extends AbstractCard> abstractCards) {
        this.abstractCards = abstractCards;
    }
    

    and then:

    someClass.setData(new ArrayList<ConcreteCard>());