Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] kotlin – How to call ArrayList in Adapter?

Posted on November 11, 2022 By

Solution 1 :

Because your data.imageUrl is an ArrayList<String>.

Try loading only the first element, since Glide can only load one image.

Glide.with(binding.root)
 .load(data.imageUrl.first())
 .into(binding.ivProductImg)

Solution 2 :

the type of data.imageUrl is ArrayList. But Glide requires a string URL or uri to load an image. You should write data.imageUrl.first() as @Lawrence mentioned above. But, also you should give a context to Glide instead of binding.root

Example:

Glide.with(mContext).load(data.imageUrl.first()).into(binding.ivProductImg)

Problem :

i have a problem here. i want to call an array string in my adapter, but the image not showing at all. and its say class com.bumptech.glide.load.engine.GlideException: Failed to load resource, com.bumptech.glide.Registry$NoModelLoaderAvailableException(Failed to find any ModelLoaders registered for model class: class java.util.ArrayList). Can someone help me?

this is the Response

data class ProductResponse (

@SerializedName("status"     ) var status     : String?           = null,
@SerializedName("produk"     ) var produk     : ArrayList<Produk> = arrayListOf(),
@SerializedName("page"       ) var page       : Int?              = null,
@SerializedName("per_page"   ) var perPage    : Int?              = null,
@SerializedName("totalRows"  ) var totalRows  : Int?              = null,
@SerializedName("totalPages" ) var totalPages : Int?              = null

) {
data class Produk (

    @SerializedName("id"          ) var id          : Int?              = null,
    @SerializedName("name"        ) var name        : String?           = null,
    @SerializedName("description" ) var description : String?           = null,
    @SerializedName("base_price"  ) var basePrice   : Int?              = null,
    @SerializedName("image_url"   ) var imageUrl    : ArrayList<String> = arrayListOf(),
    @SerializedName("location"    ) var location    : String?           = null,
    @SerializedName("user_id"     ) var userId      : Int?              = null,
    @SerializedName("createdAt"   ) var createdAt   : String?           = null,
    @SerializedName("updatedAt"   ) var updatedAt   : String?           = null,
    @SerializedName("category"    ) var category    : Category?         = Category()

) {
    data class Category (

        @SerializedName("id"   ) var id   : Int?    = null,
        @SerializedName("name" ) var name : String? = null

    )
  }
}

this is the adapter

class ProductAdapter(private val onClick: OnClickListener): RecyclerView.Adapter<ProductAdapter.ViewHolder>() {

private val diffCallBack = object: DiffUtil.ItemCallback<ProductResponse.Produk>(){
    override fun areItemsTheSame(oldItem: ProductResponse.Produk, newItem: ProductResponse.Produk): Boolean {
        return oldItem.id == newItem.id
    }
    override fun areContentsTheSame(oldItem: ProductResponse.Produk, newItem: ProductResponse.Produk): Boolean {
        return oldItem.name == newItem.name
    }
}

private val differ = AsyncListDiffer(this,diffCallBack)
fun submitData(value: List<ProductResponse.Produk>?) = differ.submitList(value)

interface OnClickListener {
    fun onClickItem (data: ProductResponse.Produk)
}

inner class ViewHolder(private val binding: ListProductHomeBinding): RecyclerView.ViewHolder(binding.root){
    @SuppressLint("SetTextI18n")
    var listCategory = ""
    fun bind (data: ProductResponse.Produk){
        Glide.with(binding.root)
            .load(data.imageUrl)
            .into(binding.ivProductImg)
        binding.tvProductName.text = data.name
        binding.tvProductCategory.text = data.category!!.name.toString()
        binding.tvProductPrice.text = currency(data.basePrice!!)
        binding.root.setOnClickListener {
            onClick.onClickItem(data)
        }
    }
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductAdapter.ViewHolder {
    val inflate = LayoutInflater.from(parent.context)
    return ViewHolder(ListProductHomeBinding.inflate(inflate,parent,false))
}

override fun onBindViewHolder(holder: ProductAdapter.ViewHolder, position: Int) {
    val data = differ.currentList[position]
    data.let {
        holder.bind(data)
    }
}

override fun getItemCount(): Int {
    return differ.currentList.size
}
}
READ  [FIXED] compare background color of layout in android application with kotlin
Powered by Inline Related Posts
Android Tags:android-recyclerview, kotlin

Post navigation

Previous Post: [FIXED] java – App crash after uploaded to play store , working correctly when installed from android studio
Next Post: What is the 21 day fitness challenge?

Related Posts

[FIXED] android – ChromeOS Emulator on Windows not available Android
[FIXED] android – Dealing with inner Transformations.switchMap on a viewModel Android
[FIXED] android – java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference Retrofit Android
[FIXED] Create android app programmatically with java code Android
[FIXED] java – Image doesn’t fill the square Android
[FIXED] android – How to understand complicated sharedflow expression? Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme