具有不同项目布局的ListView的ViewHolders

时间:2011-04-12 11:56:12

标签: android listview adapter listactivity

我有4种不同类型布局的ListView:一种是带有图像,另一种是没有图像。我尝试做this之类的事情。我重写了BaseAdapter的getView:

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

我的ViewHolders类:

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

    static class ViewHolderWithoutImage {
        TextView title;
        TextView firstParagraph;
    }

它没有第二部分,但在进入

时崩溃
holder = (ViewHolderWithImage)convertView.getTag();

部分时item.get("image_location").equals("") != truejava.lang.reflect.InvocationTargetException。任何想法我该如何解决?

2 个答案:

答案 0 :(得分:19)

我认为这是因为convertView的标签包含不同类型的ViewHolder。尝试检查convertView类型:

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。最好使用系统方法来处理不同的布局(覆盖getItemViewType())。 There is good article on this topic

答案 1 :(得分:1)

您应该覆盖适配器上的getItemViewType()getViewTypeCount(),为每种行类型返回一个不同的数字,例如ViewHolderWithoutImage为0,ViewHolderWithImage为1。这样,getView()可以正确解析要实例化的视图。