title: is there a way to change the textview text in the MainActivity whenever the text in recyclerview row changed in Kotlin
body: <p>I an trying to do a simple app, in which I use <code>RecyclerView</code>.
  Here are  my files.</p>
  
  <pre><code>MainActivity.kt
  </code></pre>
  
  <p><code>activity_main.xml</code>  --> which has <code>RecyclerView</code> (recylerViewMain)
  <code>single_row.xml</code> --> single row for recyclerViewMain</p>
  
  <p><code>MainAdapter.kt</code> --> where all bindings , <code>ViewHolder</code> and inflating</p>
  
  <p>I have also 
  <code>Product.kt</code>  --> model for Products</p>
  
  <p>Now, here what i am trying to do.</p>
  
  <p>I have added Plus and Minus buttons on the side of itemUnit and whenever i click those items it does the job i wanted, increasing the itemUnit and eventually, itemAmount</p>
  
  <p>However, this happens only on Row, and it doesn't change the Sub Total (<code>TextView</code>) in <code>main_activity.xml</code> file.</p>
  
  <p>Is there a way to change the <code>main_activity</code> <code>textView</code> whenever <code>textView</code> in the Row of <code>Recyclerview</code> changes (or whenever button clicked on the row) ?</p>
  
  <p>I am editing the code here.
  <code>Sub Total</code> amount doesn't change until I click an item button (Granola, Brownie etc.). Only after I click the these items <code>Sub Total</code>  changes and gives the updated amount.</p>
  
  <p>the interface solution didnt work for me, I think I couldnt implement it right.</p>
  
  <p>here are the codes ;</p>
  
  <pre><code>class MainActivity : AppCompatActivity(), ItemChangeListener {
  override var subTotalAmnt: Double = 30.0
  //override var subTotalAmount: Double = 50.0
  
  //override fun onItemPriceChange(20.0)
  
  lateinit var mRecyclerView : RecyclerView
  private lateinit var sDatabase: DatabaseReference
  var trId: Long = 0
  
  var discAmnt : Double = 0.00
  
  var unt = 1
  
  var tr :Trans ?= null
  
  var sb = 0.00
  var disc = sb*.1
  var tt = sb-disc
  
  var list = ArrayList&lt;Product&gt;()
  
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main2)
  
      //var subto = findViewById(R.id.txtSubAmount) as TextView
  
  
     onItemPriceChange(20.0)
      txtSubAmount.text = subTotalAmnt.toString()
  
      sDatabase = FirebaseDatabase.getInstance().getReference().child("Sales")
  
      sDatabase.child("Sales").addValueEventListener(object:ValueEventListener{
          override fun onCancelled(p0: DatabaseError) {
  
          }
  
          override fun onDataChange(p0: DataSnapshot) {
              if(p0.exists()) {
                  trId = p0.childrenCount
                  println(trId)
              }
          }
      })
      mRecyclerView = findViewById(R.id.recyclerView_main)
      mRecyclerView.layoutManager = LinearLayoutManager(this)
      mRecyclerView.adapter = MainAdapter(this, list)
  </code></pre>
  
  <p>and my Adapter Class;</p>
  
  <pre><code>class MainAdapter(val context: Context, val items : List&lt;Product&gt;) : RecyclerView.Adapter&lt;MainAdapter.PartViewHolder&gt;() {
  override fun onBindViewHolder(p0: MainAdapter.PartViewHolder, p1: Int) {
      p0.bindItems(items[p1])
  }
  
  
   var itemListener: ItemChangeListener? = null
  fun setListener(listener: ItemChangeListener) {
      this.itemListener = listener
  }
  
  override fun getItemId(position: Int): Long {
      return super.getItemId(position)
  }
  
  
  override fun getItemCount(): Int {
      return items.size
  }
  
  // Inflates the item views
  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PartViewHolder {
  
  
      // LayoutInflater: takes ID from layout defined in XML.
      // Instantiates the layout XML into corresponding View objects.
      // Use context from main app -&gt; also supplies theme layout values!
      val inflater = LayoutInflater.from(parent.context)
      // Inflate XML. Last parameter: don't immediately attach new view to the parent view group
      val view = inflater.inflate(R.layout.sinlge_row, parent, false)
  
      return PartViewHolder(view)
  
  }
  
  
  // Binds each product in the ArrayList to a view
  
  
  inner class PartViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  
  
      // Holds the TextView that will add each product to
  
  
      fun bindItems(prd: Product) {
          val textViewName = itemView.txtOrderNumber
          var textViewUnit = itemView.txtItemUnit
          val textViewPrice = itemView.txtItemPrice
          val textViewAmount = itemView.txtItemAmount
  
  
          var id = adapterPosition
  
          var unitN: Int = 1
          textViewName.text = prd.pName
          textViewUnit.text = prd.pUnit.toString()
          textViewPrice.text = prd.pPrice.toString()
          var itemPrice = prd.pPrice
          var itemAmount = itemPrice.times(unitN)
          textViewAmount.text = itemAmount.toString()
  
          itemView.btnPlus.setOnClickListener {
  
              println("item id : " + id)
  
  
  
              //itemListener = ItemChangeListener
              itemListener?.onItemPriceChange(10.0)
  
              // increase the Product model single unit
              prd.pUnit = unitN++
  
              // println("Here is the " +MainActivity().list.get(id))
  
              // bind txtItemUnit from single_row to changed unitN (single unit)
              textViewUnit.text = unitN.toString()
  
              // change the Product model single pAmount
              prd.pAmount = prd.pPrice.times(unitN)
  
              // bind txtItemAmount from single_row to Product pAmount
              textViewAmount.text = prd.pAmount.toString()
  
              //txtSubAmount.txt =
  
              //MainActivity().doSomething(subTotalAmount)
  
  
          }
  
          itemView.btnMinus.setOnClickListener(View.OnClickListener {
  
              if (unitN &gt;= 1) {
                  prd.pUnit = unitN--
                  println(prd.pUnit)
                  textViewUnit.text = unitN.toString()
  
                  textViewAmount.text = prd.pPrice.times(unitN).toString()
              } else
                  prd.pUnit = 1
              textViewUnit.text = prd.pUnit.toString()
              textViewAmount.text = prd.pPrice.times(prd.pUnit).toString()
          })
  
  
      }
  
  
  }
  }
  </code></pre>
  
  <p>and, Interface</p>
  
  <pre><code>interface ItemChangeListener {
  
  var subTotalAmnt : Double
  
  fun onItemPriceChange(subTotalAmount : Double){
  
      this.subTotalAmnt = subTotalAmount
      println("onItemPriceChange "+subTotalAmnt)
  
  }
  }
  </code></pre>
  
  <p>I am sorry for this terrible explanation as I am not native, but willing to explain more for help.
  <a href="https://i.stack.imgur.com/VkSYs.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VkSYs.jpg" alt="after clicking plus and increasing the unit"></a>
  Kind Regards.</p>
  
  <p>Edited MainAdapter</p>
  
  <pre><code>class MainAdapter(val context: Context, val items : List&lt;Product&gt;) : RecyclerView.Adapter&lt;MainAdapter.PartViewHolder&gt;() {
  override fun onBindViewHolder(p0: MainAdapter.PartViewHolder, p1: Int) {
      p0.bindItems(items[p1])
  }
  
  
  lateinit var itemListener: ItemChangeListener
  fun setListener(listener: ItemChangeListener) {
      this.itemListener = listener
  }
  
  override fun getItemId(position: Int): Long {
      return super.getItemId(position)
  }
  
  
  override fun getItemCount(): Int {
      return items.size
  }
  
  // Inflates the item views
  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PartViewHolder {
  
  
      // LayoutInflater: takes ID from layout defined in XML.
      // Instantiates the layout XML into corresponding View objects.
      // Use context from main app -&gt; also supplies theme layout values!
      val inflater = LayoutInflater.from(parent.context)
      // Inflate XML. Last parameter: don't immediately attach new view to the parent view group
      val view = inflater.inflate(R.layout.sinlge_row, parent, false)
  
      return PartViewHolder(view)
  
  }
  
  
  // Binds each product in the ArrayList to a view
  
  
  inner class PartViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  
  
      // Holds the TextView that will add each product to
  
  
      fun bindItems(prd: Product) {
          val textViewName = itemView.txtOrderNumber
          var textViewUnit = itemView.txtItemUnit
          val textViewPrice = itemView.txtItemPrice
          val textViewAmount = itemView.txtItemAmount
  
  
          var id = adapterPosition
  
          var unitN: Int = 1
          textViewName.text = prd.pName
          textViewUnit.text = prd.pUnit.toString()
          textViewPrice.text = prd.pPrice.toString()
          var itemPrice = prd.pPrice
          var itemAmount = itemPrice.times(unitN)
          textViewAmount.text = itemAmount.toString()
  
          itemView.btnPlus.setOnClickListener {
  
              println("item id : " + id)
  
  
              //itemListener = ItemChangeListener
              itemListener.onItemPriceChange(20.0)
              // increase the Product model single unit
              prd.pUnit = unitN++
  
              // println("Here is the " +MainActivity().list.get(id))
  
              // bind txtItemUnit from single_row to changed unitN (single unit)
              textViewUnit.text = unitN.toString()
  
              // change the Product model single pAmount
              prd.pAmount = prd.pPrice.times(unitN)
  
              // bind txtItemAmount from single_row to Product pAmount
              textViewAmount.text = prd.pAmount.toString()
  
              //txtSubAmount.txt =
  
              //MainActivity().doSomething(subTotalAmount)
  
  
          }
  
          itemView.btnMinus.setOnClickListener(View.OnClickListener {
  
              if (unitN &gt;= 1) {
                  prd.pUnit = unitN--
                  println(prd.pUnit)
                  textViewUnit.text = unitN.toString()
  
                  textViewAmount.text = prd.pPrice.times(unitN).toString()
              } else
                  prd.pUnit = 1
              textViewUnit.text = prd.pUnit.toString()
              textViewAmount.text = prd.pPrice.times(prd.pUnit).toString()
          })
  
  
      }
  
  
  }
   }
  </code></pre>