Instanceof没有回复正确的答案

时间:2013-07-24 09:22:01

标签: android inheritance android-listview android-arrayadapter instanceof

在Android上我有3个班级

GENERALBOOK

public class General_Book {
protected String Titolo;
protected int Tag;

public General_Book(String t, int i) {
    Titolo = t;
    Tag=i;
}

public General_Book() {
    //
}

public void setTitolo(String t) {
    Titolo=t;
}
public void setTag(int i) {
    Tag=i;
}
public String getTitolo() {
    return Titolo;
}
public int getTag() {
    return Tag;
}
}

MOSTBOOK

public class Most_Book extends General_Book {
private String SubTitolo;
private float Rating;

public Most_Book(String t,String s,float d, int c) {
    super(t,c);
    SubTitolo=s;
    Rating=d;
}
public void setSubTitolo(String t) {
    SubTitolo=t;
}
public void setRating(float c) {
    Rating=c;
}
public String getSubTitolo() {
    return SubTitolo;
}
public float getRating() {
    return Rating;
}
}

TOPBOOK

 public class Top_Book extends General_Book{
private String Incipit;
private String imageUri;
public Top_Book(String t,String i,String u, int c) {
    super(t,c);
    Incipit=i;
    imageUri=u;
}
public Top_Book() {

}
public void setIncipit(String i) {
    Incipit=i;
}
public void setImageURI(String u) {
    imageUri=u;
}
public String getIncipit() {
    return (Incipit.length()>120) ? Incipit.substring(0, 117) + "..." : Incipit;

}
public String getImageURI() {
    return imageUri;
}
  }

现在,在活动中我有2个ListView,一个是TopBook,另一个是MostBook 我创建了一个CustomArrayAdapter来管理这两种类型,但它不能像我期望的那样工作 因为总是输入“instanceof”的第一个IF,我不知道为什么,你能帮我解释一下我做错了吗?

CUSTOMARRAYADAPTER

 public class CustomArrayGeneralBook extends ArrayAdapter<General_Book> {
private final LayoutInflater mInflater;
public ImageLoader imageLoader;
public CustomArrayGeneralBook(Context context,General_Book[] tops) {
    super(context,android.R.layout.simple_list_item_2,tops);
    mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(context);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    if (convertView == null) {
        view = mInflater.inflate(R.layout.fragment_home_child_mostbook, parent, false);  
    } else {
        view = convertView;
    }
    Object o = getItem(position);
    if(o instanceof General_Book) {
        Log.e("A","QUI");
        General_Book item = getItem(position);
        view.setTag(item.getTag());
    }
    else if (o instanceof Most_Book) {
        Log.e("A","QUI2");
        Most_Book item = (Most_Book) getItem(position);
        ((TextView)view.findViewById(R.id.most_book_title)).setText(item.getTitolo());
        ((TextView)view.findViewById(R.id.most_book_subtitle)).setText(item.getSubTitolo());
        ((RatingBar)view.findViewById(R.id.most_book_ratingbar)).setRating(item.getRating());
        view.setTag(item.getTag());
    }
    else if (o instanceof Top_Book) {
        Log.e("A","QUI3");
        Top_Book item = (Top_Book) getItem(position);
        ((TextView)view.findViewById(R.id.top_book_title)).setText(item.getTitolo());
        ((TextView)view.findViewById(R.id.top_book_incipit)).setText(item.getIncipit());
        imageLoader.DisplayImage(item.getImageURI(), (ImageView)view.findViewById(R.id.top_book_cov));
        view.setTag(item.getTag());
    }       
    return view;
}
 }

修改 好吧,最后我接受了这个工作,只是为了澄清每个人:
我不得不在每个中添加一个不同的Inflater.inflate(R.layout ....)如果不崩溃app :) 谢谢

2 个答案:

答案 0 :(得分:1)

Most_Book的实例也是General_Book的一个实例,因为Most_Book扩展了General_Book。 (同样适用于Top_Book)。

在您的情况下,您需要首先测试最具体的值:

if(o instanceof Most_Book) {
     //...
} else if (o instanceof Top_Book) {
     //...
} else if (o instanceof General_Book) {
    //...
}

答案 1 :(得分:0)

我认为你需要以另一种方式放置if。因为如果oMost_BookGeneral_BookTop_Book的实例,它将输入第一个ifGeneral_Book是{的父类{1}}和Top_Book是最普遍的一个。使用专门的检查开始Most_Book检查,然后转向广义检查。

if()

参考JLS 15.20.2

  

在运行时,如果RelationalExpression的值不为null并且引用可以在不引发ClassCastException的情况下将引用(第15.16节)转换为ReferenceType,则instanceof运算符的结果为true。否则结果是错误的。

如果if(o instanceof Top_Book) { //... } else if (o instanceof Most_Book) { //... } else if (o instanceof General_Book) { //... } oTop_Book的实例,那么下面的内容就可以了。

Most_Book

因此General_Book b = o; 始终为if(o instanceof General_Book)