对不起我的noob问题。我有一个名为Card的类。 如果我使用另一个扩展名为BookHeader的Card的类。为什么我不能在List中使用它?这是我的代码:
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;
}
}
因为它显示我的课程扩展了Card.So我如何在这些代码中使用它
List<PartHeader> BookPartList = db.getAllBookParts(BookID);
BookPartAdaptet mBookPartAdapter = new BookPartAdaptet(this, BookPartList );
这里是BookPartAdaptet
public BookPartAdaptet(Context context, List<Card> cards) {
super(context, cards);
data=cards;
// inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// activity = context.getApplicationContext();
}
我在这一行得到错误
BookPartAdaptet mBookPartAdapter = new BookPartAdaptet(this, BookPartList );
表示BookPartList
myst属于List<Card>
类型而不是List<PartHeader >
我认为因为扩展类我可以使用它作为父类型?如果我不能怎么做呢? 谢谢你的帮助。对不起我的noob问题:(
答案 0 :(得分:3)
我想你想尝试这样的事情:
public BookPartAdaptet(Context context, List<? extends Card> cards) {
// ...
}
答案 1 :(得分:1)
您应该将BookPartAdaptet的定义更改为以下内容,以便您可以传递任何子类的列表
public BookPartAdaptet(Context context, List<? extends Card> cards){
}