0

I have implemented a TextWatcher for my EditText

txt_height!!.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                isExecuteSeekbar = false
            }

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun afterTextChanged(editable: Editable) {

                var height = txt_height!!.text.toString().trim { it <= ' ' }
                if (height.equals(".", ignoreCase = true))
                    txt_height!!.setText("2.0")

                height = txt_height!!.text.toString().trim { it <= ' ' }
                if (!sh!!.check_blank_data(height) && isExecute) {
                    val h = height.toFloat()
                    if (rdo_feet!!.isChecked) {
                        for (k in height_feet_lst.indices) {
                            if (height.equals(height_feet_lst[k], ignoreCase = true)) {
                                pickerFeet!!.selectedItem = k
                                break
                            }
                        }
                    } else {
                        for (k in height_cm_lst.indices) {
                            if (h == height_cm_lst[k].toFloat()) {
                                pickerCM!!.selectedItem = k
                                break
                            }
                        }
                    }
                    saveData()
                }

                isExecuteSeekbar = true
            }
        })

This line generates the error

val h = height.toFloat()

Debugging the code I noticed that the height variable is equivalent to "interface kotlin.jvm.functions.Function0", the default for the edit text is "175" but this txt_height!!.text.toString().trim { it <= ' ' } return me "interface kotlin.jvm.functions.Function0"

Maybe I did something wrong?

EDIT

Using of txt_height

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initHeight()

        body()
    }

fun initHeight() {
        heightList.clear()
        for (k in 60..240) {
            heightList.add("" + k)
        }
        val st = arrayOfNulls<CharSequence>(heightList.size)
        for (k in heightList.indices) {
            st[k] = "" + heightList[k]
        }
        picker!!.values = st
        picker!!.sideItems = 1
        picker!!.setOnItemSelectedListener { index ->
            txt_height!!.setText(st[index])
            
        }
    }

fun body(){
if (!sh!!.check_blank_data(SharedPreferencesManager.height)) {
            if (rdo_cm!!.isChecked) {
                txt_height!!.filters = arrayOf<InputFilter>(DigitsInputFilter(3, 0, 240.0))
                txt_height!!.setText(SharedPreferencesManager.height.toString())
            } else {
                txt_height!!.filters =
                    arrayOf<InputFilter>(InputFilterRange(0.00, height_feet_elements))
                txt_height!!.setText(SharedPreferencesManager.height.toString())
            }
        } else {
            if (rdo_cm!!.isChecked) {
                txt_height!!.filters = arrayOf<InputFilter>(DigitsInputFilter(3, 0, 240.0))
                txt_height!!.setText("150")
            } else {
                txt_height!!.filters =
                    arrayOf<InputFilter>(InputFilterRange(0.00, height_feet_elements))
                txt_height!!.setText("5.0")
            }
        }
}

object SharedPreferencesManager {
    private val ctx: Context
        get() = Application.instance

    private fun createSharedPreferences(): SharedPreferences {
        return ctx.getSharedPreferences(AppUtils.USERS_SHARED_PREF, Context.MODE_PRIVATE)
    }

    private val sharedPreferences by lazy { createSharedPreferences() }

    var height: String
    get() = sharedPreferences.getString(AppUtils.HEIGHT,"")!!
    set(value) = sharedPreferences.edit().putString(AppUtils.HEIGHT, value).apply()


}
8
  • Can you show the code where you set txt_height? Commented Mar 5, 2024 at 11:22
  • I've updated to add the use of txt_height Commented Mar 5, 2024 at 11:39
  • What is SharedPreferencesManager? And in particular, what is SharedPreferencesManager.height? Commented Mar 5, 2024 at 12:03
  • I've updated to add the use of SharedPreferencesManager Commented Mar 5, 2024 at 12:23
  • You've got txt_height!!.setText(SharedPreferencesManager.height.toString()) but height doesn't exist in SharedPreferencesManager, so this shouldn't be able to be compiled. Commented Mar 5, 2024 at 14:36

1 Answer 1

1

I think you want to first parse in integer then in float.

val heightInt: Int = Integer.parseInt(height)
val h = heightInt.toFloat()
Sign up to request clarification or add additional context in comments.

4 Comments

Now val heightInt: Int = Integer.parseInt(height) rise the exception java.lang.NumberFormatException: For input string: "interface kotlin.jvm.functions.Function0"
Once log the height value
after initialize with defualt -> 235, after var height = txt_height!!.text.toString().trim { it <= ' ' } -> interface kotlin.jvm.functions.Function0
This is the result of log....when I initilalize txt_height.text has value "235" but when afterTextChanged started txt_height.text has value " interface kotlin.jvm.functions.Function0"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.