Reputation: 122
I have next Adapter like this:
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()
}
}
but when I run my code, all work correctly exclude Glide
but image does't appear.
Why?
Others details of my recyclerView
work correctly, firstName
, lastName
, isOnline
fields appears all are needs detail of single view.
Upvotes: 0
Views: 917
Reputation: 413
Answer given by Sandesh is Absolutely correct and if u got any error setting an image to imageview using glide just add below line :
.error(R.drawable.your_image)
It will use image provided by you to set in an imageview if any error occured. Thank you and help me improve my answer if anything is wrong.
Upvotes: 0
Reputation: 822
Please add a listener in your glide code, that way you will get the reason(GlideException) why your image is not loading.
.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;
}
})
Upvotes: 0
Reputation: 663
Considering your items[position].avatarUri
is of type Uri, don't call the toString
method, maybe that's the problem
Upvotes: 1