Android Listview重复启动特定项目的项目

时间:2010-12-08 22:36:46

标签: android listview

在开始时,列表显示三个项目,当我向下滚动时,它会创建第四个和第五个项目,但是第六个和下一个项目没有创建。这些视图将信息与前五项混合在一起,然后重复这些视图,直到应用程序因ClassCastException崩溃。它很简单,每个项目都有不同的布局和类型,每个项目都有不同的ViewHolder。因此,由于未创建视图,ViewHolders与前五个项目相同,当列表到达具有不同ViewHolder的视图时,它会崩溃(幸运的是它发生在第十二个项目)。我需要发现为什么这些物品与第一批物品混合在一起。

这是适配器的代码,我认为这已经足够了。

public class PostsListAdapter extends BaseAdapter {
    private FacebookPost[] posts;
    private LayoutInflater mInflater;

    public PostsListAdapter (Context ctx, FacebookPost[] user_posts) {
        mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        posts = user_posts;
    }

    @Override
    public int getCount() {
        return posts.length;
    }

    @Override
    public Object getItem(int position) {
        return posts[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private abstract static class ViewHolder {
        TextView fromName;
        TextView arrow;
        TextView toName;
        TextView message;
        TextView attribution;

    }
    private static class VideoViewHolder extends ViewHolder {
        TextView name;
        TextView caption;
        TextView description;
        ImageView icon;
    }
    private static class PhotoViewHolder extends ViewHolder {
    }
    private static class LinkViewHolder extends ViewHolder {
    }
    private static class StatusViewHolder extends ViewHolder {
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        Log.d("POSITION",""+position);
        if(convertView == null) {
            switch(posts[position].getType()) {
                case FacebookPost.VIDEO:
                    Log.d(""+position,"VIDEO");
                    convertView = mInflater.inflate(R.layout.post_list_item_video, parent, false);
                    holder = new VideoViewHolder();
                    holder.fromName = (TextView)convertView.findViewById(R.id.post_list_item_video_from_name);
                    holder.arrow = (TextView)convertView.findViewById(R.id.post_list_item_video_arrow);
                    holder.toName = (TextView)convertView.findViewById(R.id.post_list_item_video_to_name);
                    holder.message = (TextView)convertView.findViewById(R.id.post_list_item_video_message);
                    ((VideoViewHolder)holder).name = (TextView)convertView.findViewById(R.id.post_list_item_video_name);
                    ((VideoViewHolder)holder).caption = (TextView)convertView.findViewById(R.id.post_list_item_video_caption);
                    ((VideoViewHolder)holder).description = (TextView)convertView.findViewById(R.id.post_list_item_video_description);
                    break;
                case FacebookPost.LINK:
                    Log.d(""+position,"LINK");
                    convertView = mInflater.inflate(R.layout.post_list_item_link, parent, false);
                    holder = new LinkViewHolder();
                    holder.fromName = (TextView)convertView.findViewById(R.id.post_list_item_link_from_name);
                    holder.arrow = (TextView)convertView.findViewById(R.id.post_list_item_link_arrow);
                    holder.toName = (TextView)convertView.findViewById(R.id.post_list_item_link_to_name);
                    holder.message = (TextView)convertView.findViewById(R.id.post_list_item_link_message);
                    break;
                case FacebookPost.STATUS:
                    Log.d(""+position,"STATUS");
                    convertView = mInflater.inflate(R.layout.post_list_item_status, parent, false);
                    holder = new StatusViewHolder();
                    holder.fromName = (TextView)convertView.findViewById(R.id.post_list_item_status_from_name);
                    holder.arrow = (TextView)convertView.findViewById(R.id.post_list_item_status_arrow);
                    holder.toName = (TextView)convertView.findViewById(R.id.post_list_item_status_to_name);
                    holder.message = (TextView)convertView.findViewById(R.id.post_list_item_status_message);
                    break;
                case FacebookPost.PHOTO:
                    Log.d(""+position,"PHOTO");
                    convertView = mInflater.inflate(R.layout.post_list_item_photo, parent, false);
                    holder = new PhotoViewHolder();
                    holder.fromName = (TextView)convertView.findViewById(R.id.post_list_item_photo_from_name);
                    holder.arrow = (TextView)convertView.findViewById(R.id.post_list_item_photo_arrow);
                    holder.toName = (TextView)convertView.findViewById(R.id.post_list_item_photo_to_name);
                    holder.message = (TextView)convertView.findViewById(R.id.post_list_item_photo_message);
                    break;
                default:
                    holder=null;
                    break;
            }
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder)convertView.getTag();
        }

        Spanned text = Html.fromHtml(posts[position].getFrom().getName());
        holder.fromName.setText(text);

        if(posts[position].getTo() != null)
            text = Html.fromHtml(posts[position].getTo()[0].getName());
        else
            text=null;
        if(text==null) {
            holder.arrow.setVisibility(View.GONE);
            holder.toName.setVisibility(View.GONE);
        } else
            holder.toName.setText(text);

        text = Html.fromHtml(posts[position].getMessage());
        holder.message.setText(text);

        switch(posts[position].getType()) {
            case FacebookPost.VIDEO:
                text = Html.fromHtml(((FacebookVideoPost)posts[position]).getCaption());
                Log.d("CAST: "+position,holder.getClass().getName());
                ((VideoViewHolder)holder).caption.setText(text);
                text = Html.fromHtml(((FacebookVideoPost)posts[position]).getName());
                ((VideoViewHolder)holder).name.setText(text);
                text = Html.fromHtml(((FacebookVideoPost)posts[position]).getDescription());
                ((VideoViewHolder)holder).description.setText(text);
                break;
            case FacebookPost.LINK:
                break;
            case FacebookPost.STATUS:
                Log.d("CAST: "+position,holder.getClass().getName());
                break;
            case FacebookPost.PHOTO:
                break;
        }

        return convertView;
    }
}

感谢您的一切。

1 个答案:

答案 0 :(得分:2)

我猜你错过了getViewTypeCount