Home > database >  Android two way data binding with float MutableLiveData
Android two way data binding with float MutableLiveData

Time:05-30

I need two way data binding from EditText to MutableLiveData. One way work fine, from LiveData to View:

@BindingAdapter("app:dtm")
fun AppCompatEditText.conversion(value: Float) {
Log.d("DTAG","Find")
setText(value.toString())
}

But the other way, from EditText to MutableLiveData I cant set:

@InverseBindingAdapter(attribute = "app:dtm", event = 
"android:textAttrChanged")
fun AppCompatEditText.conversion(): Float {
Log.d("DTAG","John Connor")
return this.text.toString().toFloat()
}

It cause loop between the two binding adapters

What am I doing wrong?

My Variable:

var a1CValue = MutableLiveData(7.0f)

View:

<androidx.appcompat.widget.AppCompatEditText
            android:id="@ id/firstText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:selectAllOnFocus="true"
            app:dtm="@={dataModel.a1CValue}"
            tools:text="1.2" />

CodePudding user response:

Found the problem. Removing MutableLiveData from the variable. And use it like so:

var a1CValue : Float? = 7f
  • Related