【问题标题】:ViewHolder not setting the string as text of a TextView. Please see detailsViewHolder 未将字符串设置为 TextView 的文本。请看详情
【发布时间】:2016-10-22 19:29:03
【问题描述】:

我在我的 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 数据库检索到的网址。

请让我知道这里出了什么问题。

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

【问题讨论】:

    标签: android firebase-realtime-database android-viewholder firebase-storage fastadapter


    【解决方案1】:

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

    在创建ViewHolder 时,还没有通过bindView 方法设置ImageUrl,因为这将在ViewHolder 创建之后调用。

    因此您必须将图像加载逻辑移至bindView 方法。 ViewHolder 只是为了让 RecyclerView 有效地缓存视图,定义侦听器以及在 RecyclerView 中使用的所有视图上保持“固定”的所有内容。

    要使您的代码正常工作,您必须进行如下更改:

    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 的请求(我也添加了此代码 sn-p )

    【讨论】:

    • 非常感谢,先生。现在,问题是它显示了这个:Cannot resolve symbol 'itemView' 将图像加载逻辑放在bindView() 方法中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 2013-10-27
    相关资源
    最近更新 更多