Android Listview repeats items starting with a specific item

at startup, the list shows three items, and when I scroll down, it creates the fourth and fifth items, but the sixth and next are not created. These views mix information with the first five elements, and they are repeated until the application terminates with a ClassCastException. The reason is simple, each element has a different structure and type, and I have a different ViewHolder for each of them. Since views are not created, ViewHolders match the first five elements, and when the list reaches one that has another ViewHolder, it breaks (it is fortunate that this happens on the twelfth element). I need to find out why the elements mix with the first.

This is the adapter code, I think it is enough.

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

.

+3
1
+2

Source: https://habr.com/ru/post/1779284/


All Articles