2

在Android上我有3類INSTANCEOF沒有返回正確的答案

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但我希望 它不起作用,因爲總是在第一次進入IF「的instanceof」的,我不知道爲什麼,你能幫我解釋我做錯了什麼嗎?

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; 
} 
} 

編輯 好吧,這在與anwser最後我接受了工作,我只想澄清大家:
我不得不添加一個不同Inflater.inflate(R.layout ....)在每個如果不會崩潰應用:)
謝謝

回答

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) { 
    //... 
} 
+0

謝謝它的作品:)等待確認您的答案 –

0

我相信你需要的if發其他方式放置。因爲如果oMost_Book,General_BookTop_Book的實例,它將輸入第一個ifGeneral_BookTop_BookMost_Book的父類,是最普遍的一類。開始if()檢查與專業的,然後走向廣義的。

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

參考JLS 15.20.2

在運行時,如果RelationalExpression的值不爲空,並參考可投(§15.16)的引用類型不instanceof運算符的結果爲真引發ClassCastException。否則結果是錯誤的。

如果oTop_BookMost_Book一個實例,則下面是好的。

General_Book b = o; 

因此if(o instanceof General_Book)永遠是true