简体   繁体   English

有没有办法在 Kotlin 中更改 recyclerview 行中的文本时更改 MainActivity 中的 textview 文本

[英]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 .我试图做一个简单的应用程序,我在其中使用RecyclerView Here are my files.这是我的文件。

MainActivity.kt

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

MainAdapter.kt --> where all bindings, ViewHolder and inflating MainAdapter.kt --> 所有绑定, ViewHolder和 inflating

I have also Product.kt --> model for Products我也有Product.kt --> model 用于产品

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我在 itemUnit 的侧面添加了加号和减号按钮,每当我单击这些项目时,它都会完成我想要的工作,增加 itemUnit 并最终增加 itemAmount

However, this happens only on Row, and it doesn't change the Sub Total ( TextView ) in main_activity.xml file.但是,这只发生在行上,它不会更改main_activity.xml文件中的小计 ( TextView )。

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)?有没有办法在Recyclerview行中的textView更改时(或每当单击该行上的按钮时)更改main_activity textView

I am editing the code here.我在这里编辑代码。 Sub Total amount doesn't change until I click an item button (Granola, Brownie etc.).在我单击一个项目按钮(格兰诺拉麦片、布朗尼等)之前, Sub Total金额不会改变。 Only after I click the these items Sub Total changes and gives the updated amount.只有在我单击这些项目Sub Total计更改并给出更新的金额后。

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;

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.我对这个可怕的解释感到抱歉,因为我不是本地人,但愿意解释更多以寻求帮助。 单击加号并增加单位后 Kind Regards.亲切的问候。

Edited MainAdapter编辑 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()
        })


    }


}
 }

I think you can do it using interface .我认为你可以使用interface来做到这一点。

  • First you create interface definition (in a separate file or in adapter).首先,您创建接口定义(在单独的文件或适配器中)。 -- for eg: interface ItemChangeListener { fun onItemPriceChange(pass itemprice or totalprice) } -- 例如: interface ItemChangeListener { fun onItemPriceChange(pass itemprice or totalprice) }
  • next you create an object of listener inside adapter接下来你在适配器内创建一个监听器的 object
  • Like lateinit var listener: ItemChangeListener inside adapter (set using fun setListener(listener:ItemChangeListener){ // code } ).lateinit var listener: ItemChangeListener inside adapter(使用fun setListener(listener:ItemChangeListener){ // code }设置)。
  • Let the main activity implement the interface.让主要活动实现接口。
  • pass the mainActivity this to the adapter.setListenermainActivity传递给adapter.setListener
  • on Clicking the button, you can call listener.onItemPriceChange(pass itemprice or totalprice)点击按钮,可以调用 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.检查此链接以获取一些详细信息。我猜也可以使用构造函数参数来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM