1

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

VETLance
  • 33
  • 1
  • 6

1 Answers1

0

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.

  • I can't use this function in my bind()function if it is okay can you be more specific to tell – VETLance Apr 15 '22 at 12:21
  • You should be able to use in bind function I am working this way in all of my projects. Can you please be more specific what is the reason you are not able ? – Dženan Bećirović Apr 15 '22 at 12:27
  • Sure maybe it will make you laugh but i am newbie and it will be my final project so dont judge me please . When i am trying to use as this Glide Glide.with(context) .load(your_url) .into(imgCurrencyIcon) 1-ı don't have context in my viewholder 2-I must use logo_url same as in "this.currencyView.text = cryptoModel.currency" because my api is just working with the same name so i can't figure it out how ı can use this Glide with my format – VETLance Apr 15 '22 at 12:29
  • No worries mate, we all have been there. So to pass context you add it to your adapter constructor and then when initializing it in your fragment or activity pass it there, if you want me I can be more specific with code example. – Dženan Bećirović Apr 15 '22 at 12:31
  • Please be more specific sorry for this – VETLance Apr 15 '22 at 12:33
  • Can you please try it like this first: Glide.with(imgCurrencyIcon.context) .load(your_url) .into(imgCurrencyIcon) – Dženan Bećirović Apr 15 '22 at 12:36
  • i tried it as with my api site and key = it didn't work and i just tried as https://s3.us-east-2.amazonaws.com/nomics-api/static/images/currencies/btc.svg for example and it didn't show in my recycler view again if you want me to update my fragment i can do it too – VETLance Apr 15 '22 at 12:42
  • I upload my fragment too again thanks for trying to help me – VETLance Apr 15 '22 at 12:45