ViewHolder 未将字符串设置为 TextView 的文本。请查看详情

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

我在我的 android 项目中使用 FastAdapter

我是这样使用它的:

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

        }
        // Manually create the ViewHolder class
        protected static class ViewHolder extends RecyclerView.ViewHolder {

            TextView imageURL;

            public ViewHolder(View itemView) {
                super(itemView);
                imageURL = (TextView)itemView.findViewById(R.id.imageURL);

if (!imageURL.getText().toString().isEmpty()) {

                Toast.makeText(itemView.getContext(), imageUID.getText().toString(), Toast.LENGTH_SHORT).show();

if (imageURL.getText().toString().startsWith("https://firebasestorage.googleapis.com/") || imageURL.getText().toString().startsWith("content://")) {
                    Picasso.with(itemView.getContext())
                            .load(imageURL.getText().toString())
                            .into(homelessImage);
                } else {
                    Toast.makeText(itemView.getContext(), "some problem", Toast.LENGTH_SHORT).show();
                }

            } else {
                Toast.makeText(itemView.getContext(), "no imageUID found", Toast.LENGTH_SHORT).show();
            }

            }
        }

    }

这里是R.layout.h_request_list_row:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              xmlns:ads="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              xmlns:tools="http://schemas.android.com/tools">

            <TextView
                android:id="@+id/imageURL"
                android:text="imageURL"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

</LinearLayout>

这里的问题是 imageURL 没有被设置为从数据库中检索到的 imageURL,而是被设置为布局文件中定义的 imageURL,因此没有图像被下载。我遵循了这个要点:https://gist.github.com/puf/f49a1b07e92952b44f2dc36d9af04e3c

我确定 String imageURL 这里:

public HRequest(String imageURL) {
            this.imageURL = imageURL;
        }

已成功获取从 Firebase 数据库中检索到的网址。

请告诉我这里出了什么问题。

抱歉问题格式不正确。我还是初学者。

问题是您尝试加载 ViewHolder 中的图像。

在创建 ViewHolder 时,尚未通过 bindView 方法设置 ImageUrl,因为这将在 [= 之后调用36=] ViewHolder 已创建。

所以你必须把图片加载逻辑移到bindView方法中。 ViewHolder 只是为了让 RecyclerView 有效地缓存视图,定义监听器以及在 RecyclerView 中使用的所有视图上保持 "fixed" 的所有内容。

要使您的代码正常工作,您必须按以下方式更改它:

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

所以基本上 RecyclerView 会调用 bindView 每次项目在列表中可见并且必须对其应用数据时。还要将 ImageView 本身添加到 ViewHolder 并且您还应该在应用新图像之前先 取消 ImageView 的请求(我也添加了这段代码)