Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android RecyclerView比较数据并根据数据更新ui_Java_Android_Kotlin_Android Recyclerview_Android Diffutils - Fatal编程技术网

Java Android RecyclerView比较数据并根据数据更新ui

Java Android RecyclerView比较数据并根据数据更新ui,java,android,kotlin,android-recyclerview,android-diffutils,Java,Android,Kotlin,Android Recyclerview,Android Diffutils,您好,我是编程新手,我刚刚创建了一个应用程序,每秒从服务器获取数据。我只想更改我的RecyclerViews持有者出价的背景,并询问位置,数据是否高于或低于之前的水平。 例如,如果物品的出价增加,则一秒钟后我的出价初始数据为4000,然后recyclerview的位置会更改其背景色 我只是实现了这段代码,但是当价格不变时,它也会随机改变背景 class Adapter(private val product: ArrayList<Products>) : RecyclerView.A

您好,我是编程新手,我刚刚创建了一个应用程序,每秒从服务器获取数据。我只想更改我的RecyclerViews持有者出价的背景,并询问位置,数据是否高于或低于之前的水平。 例如,如果物品的出价增加,则一秒钟后我的出价初始数据为4000,然后recyclerview的位置会更改其背景色

我只是实现了这段代码,但是当价格不变时,它也会随机改变背景

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)
}
}
类适配器(专用val产品:ArrayList):
RecyclerView.Adapter(){
var olddatabid:ArrayList=ArrayList()
var newdatabid:ArrayList=ArrayList()
var olddataask:ArrayList=ArrayList()
var newdataask:ArrayList=ArrayList()
趣味addNewStatutes(项目:ArrayList){
product.clear()
this.product.addAll(项目)
if(product.isNotEmpty()){
notifyDataSetChanged()
}
}
重写CreateViewHolder(父级:ViewGroup,viewType:Int):CustomViewHolder{
val itemView=LayoutInflater.from(parent.context)
.充气(R.layout.item_mcx,父项,假)
返回CustomViewHolder(itemView)
}
覆盖BindViewHolder(holder:CustomViewHolder,位置:Int){
试一试{
val基准=产品[位置]
holder.txtSymbol.text=基准面.symbol
holder.txtdate.text=datum.serExp
holder.txtBuy.text=datum.buyPrice
holder.txtSell.text=datum.sellPrice
holder.txtLtp.text=datum.lastTradedPrice
holder.txtHigh.text=基准面高度
holder.txtLow.text=datum.low
holder.txtOpen.text=datum.open
holder.txtClose.text=datum.close
holder.txtChange.text=datum.netChangeInRs
if(newdatabid.sizeolddatabid[position].toFloat()){
holder.txtBuy.setBackgroundColor(颜色.蓝色)
}
if(newdatapid[position].toFloat()olddataask[position].toFloat()){
holder.txtSell.setBackgroundColor(颜色.蓝色)
}
if(newdataask[position].toFloat()0&&product.isNotEmpty()){
产品尺寸
}否则{
0
}
}
内部类CustomViewHolder(视图:视图):RecyclerView.ViewHolder(视图){
内部变量txtSymbol:TextView=itemView.findViewById(R.id.scriptname)
内部变量txtdate:TextView=itemView.findViewById(R.id.date)
内部变量txtBuy:TextView=itemView.findViewById(R.id.buy)
内部变量txtSell:TextView=itemView.findViewById(R.id.sell)
内部变量txtLtp:TextView=itemView.findViewById(R.id.currentvalue)
内部变量txtHigh:TextView=itemView.findViewById(R.id.high)
内部变量txtLow:TextView=itemView.findViewById(R.id.low)
内部变量txtOpen:TextView=itemView.findViewById(R.id.open)
内部变量txtClose:TextView=itemView.findViewById(R.id.close)
内部变量txtChange:TextView=itemView.findViewById(R.id.change)
内部变量txtRupp:TextView=itemView.findViewById(R.id.rupp)
}
}

尝试删除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(newdatabid.size
将您的类设为数据类,并使用AsyncListDifferent.submitList更新适配器


这是一篇关于实现diffutil的好文章

我是如何避免它的问题的我的意思是删除if语句并将它们包装在**when**语句中