Search code examples
androidkotlinandroid-databindingnumberformatexception

Android Kotlin: How to save an Int from a clickListener when using DataBinding?


I am trying to switch my code from working directly with my .xml file to working through Data Binding, but I am getting NumberFormatException even though I am using the .toInt().

This is the button I had created initially, it worked and allowed me to save a customer to my local db:

    btn_save_customer.setOnClickListener {

        val customerName = customer_name.text.toString()
        val customerAge = customer_age.text.toString().toInt()

        mainViewModel.addCustomer(customerName, customerAge) // this saves the customer to db

        customer_name.text.clear()
        customer_age.text.clear()

    }

This is my current button, which works with Data Binding and crashes on every click:

        binding.apply {

        btnSaveCustomer.setOnClickListener {
            val customerNameTxt = customerName.toString()
            val customerAgeTxt = customerAge.toString().toInt()

            mainViewModel.addCustomer(customerNameTxt, customerAgeTxt) // this saves the customer to db

            customerName.clearAnimation()
            customerAge.clearAnimation()
        }
    }

And this is the error I keep getting:

java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{4358b5e VFED..CL. .F...... 30,297-690,393 #7f08008e app:id/customer_age aid=1073741825}"
    at java.lang.Integer.parseInt(Integer.java:615)
    at java.lang.Integer.parseInt(Integer.java:650)
    at com.example.observertest.MainActivity.onCreate$lambda-2$lambda-1(MainActivity.kt:57)
    at com.example.observertest.MainActivity.$r8$lambda$yqaLK6RPkbxnU-obodzyYhUCsqM(Unknown Source:0)
    at com.example.observertest.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
    at android.view.View.performClick(View.java:7448)
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
    at android.view.View.performClickInternal(View.java:7425)
    at android.view.View.access$3600(View.java:810)
    at android.view.View$PerformClick.run(View.java:28309)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7698)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)

What am I doing wrong? I made sure I input a number into the customerAge editText, but am failing to cast to string and then to Int.


Solution

  • If customerName is an EditText even if you're using data binding you have to get the text using customerName.text

    But you have one other way if you insist on using data binding.

    In your view model add this code:

    @Bindable
    var customerName = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.customerName)
        }
    

    Then in your .XML file in the customer name edit text add this:

    android:text="@={viewModel.customerName}"
    

    for importing your view model in your data binding layout use this:

    <data>
        <variable
            name="viewModel"
            type="com.example.viewmodel.mainViewModel" />
    </data>
    

    instead of using com.example.viewmodel.mainViewModel use your own address. now whenever you change the value of customerName in your view model your edit text will be updated either, and whenever you write a text in your edit text it will be stored in customer name variable in your view model.