gpt4 book ai didi

kotlin - 每当 Recyclerview 行中的文本在 Kotlin 中更改时,是否有办法更改 MainActivity 中的 textview 文本

转载 作者:行者123 更新时间:2023-12-02 13:20:10 25 4
gpt4 key购买 nike

我试图做一个简单的应用程序,我在其中使用 RecyclerView .
这是我的文件。

MainActivity.kt
activity_main.xml --> 其中有 RecyclerView (recyclerViewMain) single_row.xml --> recyclerViewMain 的单行
MainAdapter.kt --> 所有绑定(bind), ViewHolder和膨胀

我也有 Product.kt --> 产品型号

现在,这就是我想要做的。

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

但是,这只发生在行上,它不会更改 TextView 中的小计 ( main_activity.xml )文件。

有没有办法改变 main_activity textView每当 textViewRecyclerview 的行中更改(或每当按钮单击该行时)?

我在这里编辑代码。 Sub Total在我单击一个项目按钮(格兰诺拉麦片、布朗尼等)之前,金额不会改变。只有在我点击这些项目后 Sub Total更改并给出更新的金额。

接口(interface)解决方案对我不起作用,我想我无法正确实现。

这是代码;
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)

和我的适配器类;
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()
})


}


}
}

和,接口(interface)
interface ItemChangeListener {

var subTotalAmnt : Double

fun onItemPriceChange(subTotalAmount : Double){

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

}
}

我对这个可怕的解释感到抱歉,因为我不是本地人,但愿意解释更多以寻求帮助。
after clicking plus and increasing the unit
亲切的问候。

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


}


}
}

最佳答案

我认为你可以使用 接口(interface) .

  • 首先,您创建接口(interface)定义(在单独的文件或适配器中)。
    -- 例如: interface ItemChangeListener { fun onItemPriceChange(传递 itemprice 或 totalprice) }
  • 接下来在适配器
  • 中创建一个监听器对象
  • 点赞 lateinit var 监听器:ItemChangeListener 内部适配器(使用 设置 fun setListener(listener:ItemChangeListener){//代码 } )。
  • 让主要事件实现接口(interface)。
  • 通过 mainActivity 这个适配器.setListener
  • 点击按钮,可以调用 listener.onItemPriceChange(pass itemprice or totalprice)
  • 您可以通过这种方式获取主要事件的参数。

  • 检查此 link一些细节..我猜也可以使用构造函数参数来完成。

    关于kotlin - 每当 Recyclerview 行中的文本在 Kotlin 中更改时,是否有办法更改 MainActivity 中的 textview 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58800413/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com