gpt4 book ai didi

android - recyclerview仅在第二次单击Kotlin中的另一个按钮时显示

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

现在,这里的问题是,每当我单击btnCash时,它都应直接在recyclerview上显示一行。但是,只有在单击btnCash后单击另一个按钮时,它才会执行此操作。

可以说我要支付2.50。输入此值后,我单击btnCash,但它尚未显示任何内容。我必须单击另一个(数字按钮0-9)以进行显示。

通过使用println,我可以看到当我点击btnCash时,它将添加到ArrayList中。

有什么解决办法吗?
为什么第一次btnCash单击时不能显示它?

任何帮助都被提出。
我有payment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ecf0f1"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_payment"
android:layout_width="match_parent"
android:layout_height="284dp"
android:background="@color/qtr_gray6"
android:textSize="30sp"

/>


<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="280dp"
android:orientation="horizontal">


<EditText
android:id="@+id/textPayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:hint="00.0"
android:inputType="numberDecimal"
android:paddingRight="30dp"
android:textAlignment="textEnd"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="340dp"
android:orientation="horizontal">

<Button
android:id="@+id/btnFifty"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary"
android:text="£50"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnTwenty"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary"
android:text="£20"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnTen"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary"
android:text="£10"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnFivePound"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary"
android:text="£5"
android:textColor="#ecf0f1"
android:textSize="20sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="390dp"
android:orientation="horizontal">

<Button
android:id="@+id/btnOne"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary2"
android:onClick="onDigit"
android:text="1"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnTwo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary2"
android:onClick="onDigit"
android:text="2"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnThree"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary2"
android:onClick="onDigit"
android:text="3"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_btn8"
android:onClick="onBack"
android:text="Back"
android:textColor="#ecf0f1"
android:textSize="14sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="442dp"
android:orientation="horizontal">

<Button
android:id="@+id/btnFour"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary2"
android:onClick="onDigit"
android:text="4"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnFive"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary2"
android:onClick="onDigit"
android:text="5"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnSix"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary2"
android:onClick="onDigit"
android:text="6"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnStaff"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_btn9"
android:onClick="onStaff"
android:text="Staff"
android:textColor="#ecf0f1"
android:textSize="14sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="495dp"
android:orientation="horizontal">

<Button
android:id="@+id/btnSeven"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary2"
android:onClick="onDigit"
android:text="7"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnEight"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary2"
android:onClick="onDigit"
android:text="8"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnNine"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary2"
android:onClick="onDigit"
android:text="9"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnVoucher"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_btn10"

android:onClick="onVoucher"
android:text="Voucher"
android:textColor="#ecf0f1"
android:textSize="14sp" />

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="547dp"
android:orientation="horizontal">

<Button
android:id="@+id/btnZero"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary2"
android:onClick="onDigit"
android:text="0"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_primary2"
android:onClick="onClear"
android:text="cl"
android:textColor="#ecf0f1"
android:textSize="20sp" />

<Button
android:id="@+id/btnCard"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="@color/qtr_btn6"
android:onClick="onClick_Action_Function"
android:text="Card"
android:textColor="#ecf0f1"
android:textSize="30sp" />

<Button
android:id="@+id/btnCash"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:clickable="true"
android:background="@color/qtr_btn7"
android:text="Cash"
android:textColor="#ecf0f1"
android:textSize="30sp" />

</LinearLayout>

和PaymentActivity.kt
package com.soyut.su.epos01

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.Editable
import android.view.View
import android.widget.Button
import android.widget.EditText
import kotlinx.android.synthetic.main.payment.*
import android.text.TextWatcher
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import java.lang.ref.WeakReference
import java.math.BigDecimal
import java.util.*
class PaymentActivity : AppCompatActivity() {

lateinit var pRecyclerView: RecyclerView
lateinit var txtInput: EditText
var lastNumeric: Boolean = false
var stateError: Boolean = false
var lastDot: Boolean = false
var list = ArrayList<Payment>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.payment)
txtInput = textPayment
txtInput.addTextChangedListener(MoneyTextWatcher(txtInput))


pRecyclerView = recyclerView_payment
pRecyclerView.layoutManager = LinearLayoutManager(this)
pRecyclerView.adapter = PaymentAdapter(this, list)

btnCash.setOnClickListener {

var paymAmnt: String = txtInput.text.toString()
list.add(Payment("Cash", paymAmnt))


for (i in list){
println(i.paymentDesc+ " - " +i.paymentAmnt)
}

}


}

fun onClick_Action_Function(view: View) {
var paymAmnt: String = txtInput.text.toString()
list.add(Payment("Card", paymAmnt))


}

fun onDigit(view: View) {

// If not, already there is a valid expression so append to it
txtInput.append((view as Button).text)

// Set the flag
lastNumeric = true
}

fun onBack(view: View) {
var sz = txtInput.text.length
println(sz)
val textString = txtInput.getText().toString()
if (textString.length > 0) {
txtInput.setText(textString.substring(0, textString.length - 1))
}


}

fun onClear(view: View) {
var sz = txtInput.text.length
txtInput.text = txtInput.text.dropLast(sz) as Editable
}

/**
* Append . to the TextView
*/
fun onDecimalPoint(view: View) {
if (lastNumeric && !stateError && !lastDot) {
txtInput.append(".")
lastNumeric = false
lastDot = true
}
}


fun onOperator(view: View) {
if (lastNumeric && !stateError) {
txtInput.append((view as Button).text)
lastNumeric = false
lastDot = false // Reset the DOT flag
}
}

inner class MoneyTextWatcher(editText: EditText) : TextWatcher {
private val editTextWeakReference: WeakReference<EditText>

init {
editTextWeakReference = WeakReference<EditText>(editText)
}

override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}

override fun afterTextChanged(editable: Editable) {
val editText = editTextWeakReference.get() ?: return
val s = editable.toString()
if (s.isEmpty()) return
editText.removeTextChangedListener(this)
val cleanString = s.replace("[$,.]".toRegex(), "")
val parsed = BigDecimal(cleanString).setScale(2, BigDecimal.ROUND_FLOOR)
.divide(BigDecimal(100), BigDecimal.ROUND_FLOOR)
// val formatted = NumberFormat.getCurrencyInstance(Locale.UK).format(parsed)
editText.setText(parsed.toString())
editText.setSelection(parsed.toString().length)
editText.addTextChangedListener(this)
}
}


}

最后是PaymentAdapter.kt
package com.soyut.su.epos01

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.single_payment.view.*


class PaymentAdapter(val context: Context, val items: List<Payment>) : RecyclerView.Adapter<PaymentAdapter.PaymentViewHolder>() {
override fun onBindViewHolder(p0: PaymentAdapter.PaymentViewHolder, p1: Int) {
p0.bindItems(items[p1])
}


override fun getItemId(position: Int): Long {
return super.getItemId(position)
}


override fun getItemCount(): Int {
return items.size
}


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PaymentViewHolder {



val inflater = LayoutInflater.from(parent.context)

val view = inflater.inflate(R.layout.single_payment, parent, false)
view.requestFocus()



return PaymentViewHolder(view)

}


inner class PaymentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

fun bindItems(pym: Payment) {
val textPaymentDesc = itemView.txtPaymentDescription
val textPaymentAmnt = itemView.txtPaymentAmount

textPaymentDesc.text = pym.paymentDesc
textPaymentAmnt.text = pym.paymentAmnt


}


}
}


亲切的问候

最佳答案

notifyDataSetChanged()中的addupdatedelete数据之后使用data set

尝试如下

var adapter: PaymentAdapter ?=null // decleare it before onCreate()

adapter = PaymentAdapter(this, list) // here you shoule initialize you adapter
pRecyclerView.adapter = adapter // assigne your adapter to recyclerview

btnCash.setOnClickListener {

var paymAmnt: String = txtInput.text.toString()
list.add(Payment("Cash", paymAmnt))

adapter?.notifyDataSetChanged() // add this line to notify your adapter that data set is changed.

//....
}

关于android - recyclerview仅在第二次单击Kotlin中的另一个按钮时显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59265747/

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