'Android Studio Kotlin : Can't get image from API but other data works well

Hello everyone recently i want to add crypto currency api to my app

I am taking every data good. like 'symbol' 'name' 'current-price' but when it comes to

retrive icon and showing it in recycler view i am really stuck

Here is my Adapter

class CoinAdapter(var context: Context, var cryptoList: ArrayList<CoinModel>):RecyclerView.Adapter<CoinViewHolder>() {

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

override fun onBindViewHolder(holder: CoinViewHolder, position: Int) {
    holder.bind(cryptoList[position])

}

override fun getItemCount(): Int {
    return cryptoList.count()
}

}

Here is my ViewHolder :

class CoinViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

val nameView: TextView = itemView.findViewById(R.id.coinName)

val priceView: TextView = itemView.findViewById(R.id.priceUsd)

val currencyView: TextView = itemView.findViewById(R.id.coinSymbol)

var imgCurrencyIcon: ImageView = itemView.findViewById(R.id.imgCurrencyIcon)


fun bind(cryptoModel: CoinModel) {
    
    this.nameView.text = cryptoModel.name

    this.currencyView.text = cryptoModel.currency





    when {

        cryptoModel.price ?: 0.0 > 100.0 -> {
            this.priceView.text = String.format("%.0f", cryptoModel.price)
        }

        cryptoModel.price ?: 0.0 > 1.0 -> {
            this.priceView.text = String.format("%.2f", cryptoModel.price)
        }

        else -> {
            this.priceView.text = String.format("%.4f", cryptoModel.price)
        }

    }

}

} Here is my dataClass

data class CoinModel(

var currency: String?="",

var price: Double? = 0.0,

var name: String?="",

var logo_url: String = ""

)

class CoinAdapter(var context: Context, var cryptoList: ArrayList<CoinModel>):RecyclerView.Adapter<CoinViewHolder>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CoinViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.currency_item,parent,false)
    return CoinViewHolder(view)

}

override fun onBindViewHolder(holder: CoinViewHolder, position: Int) {

    holder.bind(cryptoList[position])

}

override fun getItemCount(): Int {
    return cryptoList.count()
}

}

My Fragment

CurrencyFragment : Fragment() {

private val baseUrl = "https://api.nomics.com/v1/currencies/"

private var cryptoModels: ArrayList<CoinModel>? = arrayListOf()

private var fragmentView: View? = null

private var recyclerView: RecyclerView? = null

private var coinAdapter: CoinAdapter? = null

//https://api.nomics.com/v1/currencies/ticker?key=9aaf5b50f60d58d38fab838fe5b37aa3175f7d52

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    if (fragmentView == null) {
        fragmentView = inflater.inflate(R.layout.fragment_currency, container, false)
    }

    return fragmentView
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    recyclerView = view.findViewById(R.id.coin_recycler_view)

    coinAdapter = context?.let { CoinAdapter(it, arrayListOf()) }

    recyclerView?.layoutManager =
        LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

    recyclerView?.adapter = coinAdapter

    loadData()

}

private fun loadData() {

    val retrofit = Retrofit.Builder()
        .baseUrl(baseUrl)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val service = retrofit.create(CryptoAPI::class.java)

    val call = service.getData()

    call.enqueue(object : Callback<List<CoinModel>> {
        @SuppressLint("NotifyDataSetChanged")
        override fun onResponse(
            call: Call<List<CoinModel>>,
            response: Response<List<CoinModel>>
        ) {
            if (response.isSuccessful) {

                response.body()?.let {
                    cryptoModels = ArrayList(it)

                    coinAdapter?.cryptoList = cryptoModels ?: arrayListOf()

                    coinAdapter?.notifyDataSetChanged()

                }
            }

        }

        override fun onFailure(call: Call<List<CoinModel>>, t: Throwable) {

            t.printStackTrace()

        }

    })

}

}

Note: I didn't paste my method for imageView because i can't figure it out. name text and symbol is perfectly fine and working. Note2: API sends me icons as 'svg' format so i can't use Fresco too. Please help me to find solution on this



Solution 1:[1]

You can use glide library and it will look like this:

Glide.with(context)
  .load(your_url)
  .into(imgCurrencyIcon)

You add this to your bind() function.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dženan Be?irovi?