您好,我是编程新手,我刚刚创建了一个应用程序,每秒从服务器获取数据,我只想更改我的 RecyclerViews 持有者出价的背景,并询问数据是否比以前的数据更好或更低。例如,如果我的项目出价增加,然后 recyclerview 的位置改变它的背景颜色,我的出价初始数据在一秒钟后是 4000。
我只是实现了这段代码,但是当价格没有改变时,它也会随机改变背景。
class Adapter(private val product: ArrayList<Products>) :
RecyclerView.Adapter<McxAdapter.CustomViewHolder>() {
var olddatabid: ArrayList<String> = ArrayList()
var newdatabid: ArrayList<String> = ArrayList()
var olddataask: ArrayList<String> = ArrayList()
var newdataask: ArrayList<String> = ArrayList()
fun addNewStatutes(items: ArrayList<Products>) {
product.clear()
this.product.addAll(items)
if (product.isNotEmpty()) {
notifyDataSetChanged()
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.item_mcx, parent, false)
return CustomViewHolder(itemView)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
try {
val datum = product[position]
holder.txtSymbol.text = datum.symbol
holder.txtdate.text = datum.serExp
holder.txtBuy.text = datum.buyPrice
holder.txtSell.text = datum.sellPrice
holder.txtLtp.text = datum.lastTradedPrice
holder.txtHigh.text = datum.high
holder.txtLow.text = datum.low
holder.txtOpen.text = datum.open
holder.txtClose.text = datum.close
holder.txtChange.text = datum.netChangeInRs
if (newdatabid.size < product.size) {
newdatabid.add(datum.buyPrice.toString())
}
if (olddatabid.size < product.size) {
olddatabid.add(datum.buyPrice.toString())
}
if (newdataask.size < product.size) {
newdataask.add(datum.sellPrice.toString())
}
if (olddataask.size < product.size) {
olddataask.add(datum.sellPrice.toString())
}
newdatabid[position] = datum.buyPrice.toString()
newdataask[position] = datum.sellPrice.toString()
if (newdatabid[position].toFloat() > olddatabid[position].toFloat()) {
holder.txtBuy.setBackgroundColor(Color.BLUE)
}
if (newdatabid[position].toFloat() < olddatabid[position].toFloat()) {
holder.txtBuy.setBackgroundColor(Color.RED)
}
if (newdataask[position].toFloat() > olddataask[position].toFloat()) {
holder.txtSell.setBackgroundColor(Color.BLUE)
}
if (newdataask[position].toFloat() < olddataask[position].toFloat()) {
holder.txtSell.setBackgroundColor(Color.RED)
}
olddatabid[position] = newdatabid[position]
olddataask[position] = newdataask[position]
} catch (e: Exception) {
}
}
override fun getItemCount(): Int {
return if (product.size > 0 && product.isNotEmpty()) {
product.size
} else {
0
}
}
inner class CustomViewHolder(view: View) : RecyclerView.ViewHolder(view) {
internal var txtSymbol: TextView = itemView.findViewById(R.id.scriptname)
internal var txtdate: TextView = itemView.findViewById(R.id.date)
internal var txtBuy: TextView = itemView.findViewById(R.id.buy)
internal var txtSell: TextView = itemView.findViewById(R.id.sell)
internal var txtLtp: TextView = itemView.findViewById(R.id.currentvalue)
internal var txtHigh: TextView = itemView.findViewById(R.id.high)
internal var txtLow: TextView = itemView.findViewById(R.id.low)
internal var txtOpen: TextView = itemView.findViewById(R.id.open)
internal var txtClose: TextView = itemView.findViewById(R.id.close)
internal var txtChange: TextView = itemView.findViewById(R.id.change)
internal var txtRupp: TextView = itemView.findViewById(R.id.rupp)
}
}