ViewHolders for ListViews with different item layouts
Asked Answered
P

2

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?

Predecessor answered 12/4, 2011 at 11:56 Comment(1)
Try using getCause() or getTargetException() (see here: download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/…) to get more info regarding the exception.Levison
D
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.

Davidson answered 12/4, 2011 at 12:31 Comment(1)
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.item2Mefford
N
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.

Numbles answered 17/7, 2014 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.