开发者

ViewHolders for ListViews with different item layouts

I have ListView with 2 different types of layout: one 开发者_StackOverflow中文版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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜