ViewHolder not setting the string as text of a TextView. Please see details

前提是你 提交于 2019-12-06 05:54:05

The problem is that you try to load the image in the ViewHolder.

At the time when the ViewHolder is created there was not yet an ImageUrl set via the bindView method, as this will be called after the ViewHolder was created.

So you have to move the image loading logic to the bindView method. The ViewHolder is just there for the RecyclerView to efficiently cache the views, have listeners defined and everything which keeps "fixed" over all the view's which are used in the RecyclerView.

To get your code working you have to change it as following:

public class HRequest extends AbstractItem < HRequest, HRequest.ViewHolder > {
    public String imageURL;
    public HRequest() {}
    public HRequest(String imageURL) {
        this.imageURL = imageURL;
    }
    // Fast Adapter methods
    @Override
    public int getType() {
        return R.id.recycler_view;
    }
    @Override
    public int getLayoutRes() {
        return R.layout.h_request_list_row;
    }
    @Override
    public void bindView(ViewHolder holder) {
        super.bindView(holder);
        holder.imageURL.setText(imageURL);

        if (!imageURL.isEmpty()) {
            Toast.makeText(holder.itemView.getContext(), imageURL, Toast.LENGTH_SHORT).show();
            if (imageURL.startsWith("https://firebasestorage.googleapis.com/") || imageURL.startsWith("content://")) {
                Picasso.with(holder.itemView.getContext()).cancelRequest(holder.myImageView);
                Picasso.with(holder.itemView.getContext()).load(imageURL).into(holder.myImageView);
            } else {
                Toast.makeText(holder.itemView.getContext(), "some problem", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(holder.itemView.getContext(), "no imageUID found", Toast.LENGTH_SHORT).show();
        }
    }
    // Manually create the ViewHolder class
    protected static class ViewHolder extends RecyclerView.ViewHolder {
        TextView imageURL;
        ImageView myImageView;
        public ViewHolder(View itemView) {
            super(itemView);
            imageURL = (TextView) itemView.findViewById(R.id.imageURL);
            myImageView = (ImageView) itemView.findViewById(R.id.myImageView);
        }
    }
}

So basically the RecyclerView will call bindView each time the item will come visible in the list and the data has to be applied to it. Also add the ImageView itself to the ViewHolder and you should also cancel the request on the ImageView with picasso first before a new image is getting applied (I added this code snippet too)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!