如何在RecyclerViewAdapter中使用TextWatcher

时间:2018-10-15 17:18:18

标签: android android-recyclerview kotlin android-adapter

我有一个回收站视图,这是物品。

enter image description here

适配器几乎可以正常工作。问题是当我尝试使用EditText添加数量时,它不起作用。我尝试添加TextWatcher,但没有任何反应。我认为我没有正确使用它。这是我的适配器内的类:

<i>

我尝试了很多方法,但是仍然无法正常工作。现在有一个NullPointerException。关于如何解决的任何想法?

更新:

这是LogCat

class ProductoViewHolder(view: View) : RecyclerView.ViewHolder(view) {

    var nombreProducto: TextView = view.tv_producto_seleccion
    var especialidadProducto: TextView = view.tv_especialidad_producto
    var btnMinus: ImageView = view.img_minus
    var btnPlus: ImageView = view.img_plus
    var cantidadProducto: EditText = view.tv_producto_cantidad
    var layoutWrapper: LinearLayout = view.layoutWrapper
    var checkBoxProductos: CheckBox = view.cb_productos

    fun bindItem(producto: Producto) {
        nombreProducto.text = producto.nombre
        especialidadProducto.text = producto.categoria

        btnPlus.setOnClickListener {
            producto.cantidad += 1
            cantidadProducto.setText(producto.cantidad.toString())
        }

        btnMinus.setOnClickListener {
            if (producto.cantidad > 0) {
                producto.cantidad -= 1
            } else if (producto.cantidad == 0) {
                val mySnackbar = Snackbar.make(itemView, "Ya no puedes disminuir la cantidad", Snackbar.LENGTH_LONG)
                mySnackbar.show()
            }
            cantidadProducto.setText(producto.cantidad.toString())
        }

        checkBoxProductos.isChecked = producto.checked
        cantidadProducto.setText(producto.cantidad.toString())

        if (producto.checked) {
            btnPlus.setImageResource(R.drawable.ic_plus)
            btnPlus.isEnabled = true
            btnMinus.setImageResource(R.drawable.ic_minus)
            btnMinus.isEnabled = true
            cantidadProducto.isEnabled = true


            //this is the TextWatcher//
            if(cantidadProducto.isEnabled){
                cantidadProducto.addTextChangedListener(object : TextWatcher{
                    override fun afterTextChanged(s: Editable?) {
                    }

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

                    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                        producto.cantidad = s.toString().trim().toInt()
                    }

                })
            }else{
                cantidadProducto.addTextChangedListener(null)
            }


        } else {
            btnPlus.setImageResource(R.drawable.ic_plus_disabled)
            btnPlus.isEnabled = false
            btnMinus.setImageResource(R.drawable.ic_minus_disabled)
            btnMinus.isEnabled = false
            cantidadProducto.isEnabled = false

            cantidadProducto.addTextChangedListener(null)

        }

        checkBoxProductos.setOnClickListener {

            producto.checked = checkBoxProductos.isChecked

            btnPlus.isEnabled = checkBoxProductos.isChecked
            btnMinus.isEnabled = checkBoxProductos.isChecked

            if (checkBoxProductos.isChecked) {
                btnPlus.setImageResource(R.drawable.ic_plus)
                btnPlus.isEnabled = true
                btnMinus.setImageResource(R.drawable.ic_minus)
                btnMinus.isEnabled = true
                cantidadProducto.isEnabled = true


            } else {
                btnPlus.setImageResource(R.drawable.ic_plus_disabled)
                btnPlus.isEnabled = false
                btnMinus.setImageResource(R.drawable.ic_minus_disabled)
                btnMinus.isEnabled = false
                cantidadProducto.setText("0")
                cantidadProducto.isEnabled = false

                producto.cantidad = 0

            }

        }


    }

}

这是持有人的XML

Process: syscomestic.theapplabperu.com.syscosmetic, PID: 13508
java.lang.NullPointerException: Attempt to invoke interface method 'void android.text.TextWatcher.beforeTextChanged(java.lang.CharSequence, int, int, int)' on a null object reference
    at android.widget.TextView.sendBeforeTextChanged(TextView.java:8450)
    at android.widget.TextView.access$1300(TextView.java:253)
    at android.widget.TextView$ChangeWatcher.beforeTextChanged(TextView.java:10690)
    at android.text.SpannableStringBuilder.sendBeforeTextChanged(SpannableStringBuilder.java:1038)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:534)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:503)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:39)
    at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
    at android.widget.TextView.doKeyDown(TextView.java:6396)
    at android.widget.TextView.onKeyDown(TextView.java:6209)
    at android.view.KeyEvent.dispatch(KeyEvent.java:2702)
    at android.view.View.dispatchKeyEvent(View.java:9328)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1679)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1679)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1679)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1679)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1679)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1679)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1679)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1679)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1679)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1679)
    at com.android.internal.policy.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2445)
    at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1758)
    at android.app.Activity.dispatchKeyEvent(Activity.java:2759)
    at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
    at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
    at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)
    at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
    at com.android.internal.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2353)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4659)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4615)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4111)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4164)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4130)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4276)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4138)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4333)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4111)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4164)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4130)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4138)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4111)
    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6593)
    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6567)
    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6510)
    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3891)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5728)
    at java.lang.reflect.Method.invoke(Native Method)

3 个答案:

答案 0 :(得分:0)

更改此部分:

    if(cantidadProducto.isEnabled){
        cantidadProducto.addTextChangedListener(object : TextWatcher{
            override fun afterTextChanged(s: Editable?) {
            }

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

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                producto.cantidad = s.toString().trim().toInt()
            }

        })
    }else{
        cantidadProducto.addTextChangedListener(null)
    }

收件人:

cantidadProducto.addTextChangedListener(object : TextWatcher {
    override fun afterTextChanged(s: Editable?) {
    }

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

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        if(cantidadProducto.isEnabled)
            producto.cantidad = s.toString().trim().toInt()
    }

})

因此您不必将文本监视程序设置为null并查看会发生什么情况。

答案 1 :(得分:0)

尝试此代码,让我知道它是否有效

      cantidadProducto.addTextChangedListener(object : TextWatcher{
            override fun afterTextChanged(s: Editable?) {
           if(cantidadProducto.isEnabled){
             producto.cantidad = s.toString().trim().toInt()
             notifyDatasetChanged()
             }
            }

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

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

            }

        })

答案 2 :(得分:0)

NullPointerException是因为在else语句(cantidadProducto.addTextChangedListener(null))中为EditText的文本监视程序分配了空值 如果您不希望任何TextWatcher将一个空的侦听器添加到cantidadProducto而不是将其分配为空

相关问题