Majid Hojati
Majid Hojati

Reputation: 1790

extended class is not of parent type?

Sorry for my noob question.I have a class named of type Card. If I used another class that extends Card named BookHeader.Why I can not use it in a List?here is my code:

    package com.peomtime.tosca.peomtime.Databse;

import android.content.Context;

import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardHeader;

/**
 * Created by CrazyVirus on 2015-02-06.
 */
public class PartHeader extends Card {
    private String Title;
    private String ID;
    private String BookID;
    private Context context;
    private CardHeader header;

    public void setBookID(String BookID)
    {
        this.BookID = BookID;
    }
    public String getBookID(){return this.BookID;}

    public void setTitle(String Title)
    {
        this.Title = Title;
        header.setTitle(Title);
        this.addCardHeader(header);
    }
    public String getTitle(){return this.Title;}

    public void setID(String ID)
    {
        this.ID = ID;
    }
    public String getID(){return this.ID;}

    public PartHeader(Context c){
        super(c);
        this.context =c;
        header = new CardHeader(context);

    }

    public Card getCard(){

        return this;
    }
}

As it shows my class extends Card.So How can I use it in these code

List<PartHeader> BookPartList = db.getAllBookParts(BookID);
BookPartAdaptet mBookPartAdapter = new BookPartAdaptet(this, BookPartList );

here is BookPartAdaptet

  public BookPartAdaptet(Context context, List<Card> cards) {


    super(context, cards);
    data=cards;
   // inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  //  activity = context.getApplicationContext();

}

I get error in this line

BookPartAdaptet mBookPartAdapter = new BookPartAdaptet(this, BookPartList );

that says BookPartList myst be of type List<Card> not List<PartHeader >

I think because of extended class I can use it as parents type?If I can not how Can do it? Thank you for your help.And sorry for my noob question:(

Upvotes: 1

Views: 76

Answers (2)

vsr
vsr

Reputation: 63

You should change you definition of BookPartAdaptet to the follwoing so you can pass the list of any subclass of Cards

public BookPartAdaptet(Context context, List<? extends Card> cards){
}

Upvotes: 1

jadhachem
jadhachem

Reputation: 1123

I think you want to try something like this:

public BookPartAdaptet(Context context, List<? extends Card> cards) {
    // ...
}

Upvotes: 3

Related Questions