5

I have ListView with 2 different types of layout: one with image and another without image. I try to do something like this. I override getView of BaseAdapter:

public View getView(int position, View convertView, ViewGroup parent) {
        Map<String, String> item = mData.get(position);
        if(item.get("image_location").equals("") == true){
            ViewHolderWithoutImage holder = new ViewHolderWithoutImage();
            if(convertView == null){
                convertView = mInflater.inflate(R.layout.row_without_image, null);
                holder.title = (TextView)convertView.findViewById(R.id.title);
                holder.firstParagraph = (TextView)convertView.findViewById(R.id.first_paragraph);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolderWithoutImage)convertView.getTag();
            }
            holder.title.setText(mData.get(position).get("title").toString());
            holder.firstParagraph.setText(item.get("first_paragraph").toString());

        }else{
            ViewHolderWithImage holder = new ViewHolderWithImage();
            Bitmap bm = null;
            if(convertView == null){
                convertView = mInflater.inflate(R.layout.row_with_image, null);
                holder.title = (TextView)convertView.findViewById(R.id.title_image);
                holder.firstParagraph = (TextView)convertView.findViewById(R.id.first_paragraph_image);
                holder.image = (ImageView)convertView.findViewById(R.id.image);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolderWithImage)convertView.getTag();
            }

            holder.title.setText(mData.get(position).get("title").toString());
            holder.firstParagraph.setText(item.get("first_paragraph").toString());
            String location = imageBaseUrl + item.get("image_location");
            bm = downloadImage(location);
            holder.image.setImageBitmap(bm);
        }
        return convertView;
    }

My ViewHolders classes:

static class ViewHolderWithImage {
        TextView title;
        TextView firstParagraph;
        ImageView image;
    }

    static class ViewHolderWithoutImage {
        TextView title;
        TextView firstParagraph;
    }

It works without the second part, but crashes when it going to

holder = (ViewHolderWithImage)convertView.getTag();

in part when item.get("image_location").equals("") != true with java.lang.reflect.InvocationTargetException. Any ideas how can I fix that?

Community
  • 1
  • 1
shtkuh
  • 349
  • 1
  • 10
  • 22
  • 1
    Try using getCause() or getTargetException() (see here: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/InvocationTargetException.html) to get more info regarding the exception. – MByD Apr 12 '11 at 12:03

2 Answers2

19

I think it's happens because convertView's tag holds ViewHolder with different type. Try to check convertView type:

if(item.get("image_location").equals("") == true){
    ...
    if(convertView == null || !(convertView.getTag() instanceof ViewHolderWithoutImage)){
    ...
}else{
    ...
    if(convertView == null || !(convertView.getTag() instanceof ViewHolderWithImage)){
        convertView = mInflater.inflate(R.layout.row_with_image, null);
        ...

P.S. It's better to use system methods for handling different layouts (override getItemViewType()). There is good article on this topic.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
  • The last piece of code of the article is not correct. He's not managing this case for example : convertView != null and currently instanciated to R.layout.item1 but current positon need R.layout.item2 – Sébastien BATEZAT Jun 09 '15 at 19:34
1

You should override getItemViewType() and getViewTypeCount() on your Adapter, returning a distinct number for each row type, like 0 for ViewHolderWithoutImage and 1 for ViewHolderWithImage. This way, getView() could resolve correctly which view to instantiate.

mugiwarapy
  • 561
  • 4
  • 4