1

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:(

Majid Hojati
  • 1,740
  • 4
  • 29
  • 61
  • Why are you defining list with specific type i.e. `List` then why not `List list = ..`? – SMA Feb 07 '15 at 07:30
  • @almasshaikh Because I need a new extended calss of `Card ` and BookPartAdaptet needs a List and I have List ,Can not I cast them?or things like that? – Majid Hojati Feb 07 '15 at 07:33

2 Answers2

3

I think you want to try something like this:

public BookPartAdaptet(Context context, List<? extends Card> cards) {
    // ...
}
jadhachem
  • 1,123
  • 2
  • 11
  • 19
  • 1
    You beat me. An explanation for this is here: http://stackoverflow.com/questions/2723397/java-generics-what-is-pecs – Axel Feb 07 '15 at 07:33
  • It seems this is what I need.When I use List extends Card> it meens that I can use extended classes?then what will happen next?do I face a problem when another class needs Card and I give them PartHeader that is extended from Card? – Majid Hojati Feb 07 '15 at 07:36
  • @Axel I used your sugested way and it fixed but now I have the same problem in `super(context, cards);` should I then convert List to List? – Majid Hojati Feb 07 '15 at 07:41
  • As described in the link provided by @Axel, passing `List extends Card>` means that you can use any element in the list as though it were a `Card`, even if its true type is any subclass of `Card`. If you give a `PartHeader` object to another method that is expecting a `Card`, then that is still fine. I'm not sure what you mean by the `super(context,cards);` part; we would need more details to understand the problem. – jadhachem Feb 07 '15 at 12:11
  • @jadhachem Thank you for your help,you know I have extended `BookPartAdaptet` from another class and the `BookPartAdaptet` method is overrided so in `super(context,cards);` I still forced to add `card` type – Majid Hojati Feb 07 '15 at 16:39
  • Oh I see. Does the corresponding constructor in the super-class of `BookPartAdaptet` also take `(Context context, List cards)` argument? If so maybe you should also make it `List extends Card> cards`. – jadhachem Feb 07 '15 at 19:24
1

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){
}
vsr
  • 63
  • 1
  • 11