Search code examples
androidinheritanceandroid-listviewandroid-arrayadapterinstanceof

Instanceof doesn't return right answer


On Android I have 3 class

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

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

Now on activity I have 2 ListView one with a TopBook and another one with MostBook I created a CustomArrayAdapter that manages both type but it doesn't work as I expect Because always enter in first IF of "instanceof", and I have no idea why, could you help me and explain what I'm doing wrong?

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

EDIT Ok, that works at the end with anwser I accepted, just to clarify everyone:
I had to add a different Inflater.inflate(R.layout....) in each if to do not crash app :)
Thank you


Solution

  • An instance of Most_Book is also an instance of General_Book, because Most_Book extends General_Book. (same goes for Top_Book).

    In your case, you need to test for the most specific values first:

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