[英]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大家好,最近我想在我的应用程序中添加加密货币 api
I am taking every data good.我把每一个数据都做好了。 like 'symbol' 'name' 'current-price' but when it comes to
像'symbol''name''current-price'但是当涉及到
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:这是我的 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.注意:我没有粘贴 imageView 的方法,因为我无法弄清楚。 name text and symbol is perfectly fine and working.
名称文本和符号非常好并且可以正常工作。 Note2: API sends me icons as 'svg' format so i can't use Fresco too.
注意 2:API 将图标作为“svg”格式发送给我,所以我也不能使用 Fresco。 Please help me to find solution on this
请帮我找到解决方案
You can use glide library and it will look like this:你可以使用 glide 库,它看起来像这样:
Glide.with(context)
.load(your_url)
.into(imgCurrencyIcon)
You add this to your bind() function.您将其添加到您的 bind() function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.