Glide 不会将图像放入 imageView

Glide does't put image into imageView

我有下一个这样的适配器:

class ChatAdapter(chatItems: List<User>,
                  context: MainActivity) : RecyclerView.Adapter<ChatAdapter.ChatItemHolder>() {

    private val items= chatItems
    private val context = context

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatItemHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.chat_item, parent, false)
        return ChatItemHolder(itemView)
    }

    override fun getItemCount(): Int = items.size

    override fun onBindViewHolder(holder: ChatItemHolder, position: Int) {
        holder.firstName?.text = items[position].firstName
        holder.lastName?.text = items[position].lastName
        holder.initials?.text = setInitials(items[position].firstName, items[position].lastName )
        Log.d("__test__", items[position].avatarUri.toString())

     // Probleme is here
    Glide.with(context)
                .load(items[position].avatarUri.toString())
                .centerCrop()
                .into(holder.avatar)
        holder.isOnline?.visibility = if (items[position].online) View.VISIBLE else View.GONE
    }

    inner class ChatItemHolder(chatItem: View) : RecyclerView.ViewHolder(chatItem){
        var firstName: TextView? = null
        var lastName: TextView? = null
        var initials: TextView? = null
        var avatar: ImageView
        var isOnline: View? = null
        init {
            firstName = chatItem.findViewById(R.id.tv_first_name)
            lastName =chatItem.findViewById(R.id.tv_last_name)
            initials = chatItem.findViewById(R.id.tv_initials)
            avatar = chatItem.findViewById(R.id.iv_avatar)
            isOnline = chatItem.findViewById(R.id.online)
        }
    }

    private fun setInitials(fn: String?, ln: String?): String {
        return if (ln.isNullOrEmpty())
            fn!!.first().toString() else fn!!.first().toString() + ln.first()
    }
}

但是当我 运行 我的代码时,所有工作正常排除 Glide 但图像没有出现。 为什么? 我的 recyclerView 的其他细节工作正常,firstNamelastNameisOnline 字段出现都是需要单一视图的细节。

考虑到您的 items[position].avatarUri 是 Uri 类型,请不要调用 toString 方法,也许这就是问题所在

Sandesh 给出的答案绝对正确,如果您在使用 glide 将图像设置为 imageview 时遇到任何错误,只需添加以下行:

.error(R.drawable.your_image)

如果出现任何错误,它将使用您提供的图像在图像视图中进行设置。 谢谢你,如果有任何错误,请帮助我改进我的答案。

请在您的滑行代码中添加一个侦听器,这样您将了解您的图像未加载的原因(GlideException)。

.listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                 //on load failed
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    //on load success
                    return false;
                }
        })