【问题标题】:Android RecyclerView Compare the data and update ui acording to itAndroid RecyclerView 比较数据并根据它更新ui
【发布时间】:2020-01-24 14:48:43
【问题描述】:

您好,我是编程新手,我刚刚创建了一个应用程序,它每秒从服务器获取数据,我只想更改我的 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)
}
}

【问题讨论】:

    标签: java android kotlin android-recyclerview android-diffutils


    【解决方案1】:

    尝试去掉 if else 或 when 语句中的 if 语句,然后查看结果

     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())
            }
    
    

    【讨论】:

    • 我怎么没有得到它
    • 对不起,我的意思是删除 if 语句并将它们包装在** when** 语句中
    【解决方案2】:

    让你的类成为数据类并使用 AsyncListDiffer.submitList 来更新适配器

    这里有一篇关于实现diffutil的好文章https://medium.com/@iammert/using-diffutil-in-android-recyclerview-bdca8e4fbb00

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多