2

我在我的android项目中使用FastAdapterViewHolder不将该字符串设置为TextView的文本。请看详情

这是我如何使用它:

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

已经成功地提取从火力地堡数据库中检索到的URL。

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

对不起,格式不正确的问题。我仍然是这里的初学者。

回答

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与毕加索是越来越应用新的图像前先请求(我加了这个代码片断太)

+0

非常感谢,主席先生。现在,问题在于它显示了这种情况:'在将图像加载逻辑放入'bindView()'方法时,无法解析符号'itemView'。 –

+1

调整了代码 – mikepenz

相关问题