0

Hello I am New to Programming i just created an application that get the data from server every second i just wants to changethe background of my RecyclerViews holder's bid and ask position if the data if grater or lower then previous. for example my init data at bid price is 4000 after one second if my bid price of the item is increase and then the position at recyclerview's change the background color of it.

i just implement this code but its randomly changing the background also when the price are not changed.

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)
}
}
user12575366
  • 303
  • 1
  • 2
  • 8

2 Answers2

0

Try to remove if statements in if else or when statement, then check the results

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

0

Make your class a data class and use AsyncListDiffer.submitList to update adapter

Here is a good article about implementing diffutil https://medium.com/@iammert/using-diffutil-in-android-recyclerview-bdca8e4fbb00