David Green
David Green

Reputation: 83

is there a way to change the textview text in the MainActivity whenever the text in recyclerview row changed in Kotlin

I an trying to do a simple app, in which I use RecyclerView. Here are my files.

MainActivity.kt

activity_main.xml --> which has RecyclerView (recylerViewMain) single_row.xml --> single row for recyclerViewMain

MainAdapter.kt --> where all bindings , ViewHolder and inflating

I have also Product.kt --> model for Products

Now, here what i am trying to do.

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

However, this happens only on Row, and it doesn't change the Sub Total (TextView) in main_activity.xml file.

Is there a way to change the main_activity textView whenever textView in the Row of Recyclerview changes (or whenever button clicked on the row) ?

I am editing the code here. Sub Total amount doesn't change until I click an item button (Granola, Brownie etc.). Only after I click the these items Sub Total changes and gives the updated amount.

the interface solution didnt work for me, I think I couldnt implement it right.

here are the codes ;

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


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)

and my Adapter Class;

class MainAdapter(val context: Context, val items : List<Product>) : RecyclerView.Adapter<MainAdapter.PartViewHolder>() {
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 -> 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 >= 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()
        })


    }


}
}

and, Interface

interface ItemChangeListener {

var subTotalAmnt : Double

fun onItemPriceChange(subTotalAmount : Double){

    this.subTotalAmnt = subTotalAmount
    println("onItemPriceChange "+subTotalAmnt)

}
}

I am sorry for this terrible explanation as I am not native, but willing to explain more for help. after clicking plus and increasing the unit Kind Regards.

Edited MainAdapter

class MainAdapter(val context: Context, val items : List<Product>) : RecyclerView.Adapter<MainAdapter.PartViewHolder>() {
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 -> 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 >= 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()
        })


    }


}
 }

Upvotes: 1

Views: 246

Answers (1)

Monster Brain
Monster Brain

Reputation: 2119

I think you can do it using interface.

  • First you create interface definition (in a separate file or in adapter). -- for eg: interface ItemChangeListener { fun onItemPriceChange(pass itemprice or totalprice) }
  • next you create an object of listener inside adapter
  • Like lateinit var listener: ItemChangeListener inside adapter (set using fun setListener(listener:ItemChangeListener){ // code } ).
  • Let the main activity implement the interface.
  • pass the mainActivity this to the adapter.setListener
  • on Clicking the button, you can call listener.onItemPriceChange(pass itemprice or totalprice)
  • you can get the parameter on main activity in this way.

Check this link for some details.. Same can be done using constructor parameter I guess.

Upvotes: 2

Related Questions