Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot bind double with Android databinding

I have a class that extends BaseObservable, this class contains the double value

This is an example :

public class BindedValue extends BaseObservable {

public double value;


public TextWatcher setValue = new TextWatcherAdapter(){

    @Override
    public void afterTextChanged(Editable s) {
        value = Double.parseDouble(s.toString());
    }
  };
}

Then I got xml

<data class="net.example.util.Value">

            <variable
        name="BindedValue"
        type="net.makereal.makerealmaquette.domain.BindedValue"/>

    <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:hint="Value"
                android:addTextChangedListener="@{BindedValue.setValue}"
                android:inputType="numberDecimal"
                android:text="@{BindedValue.value}"/>

When I try to run or build the app I get this error :

Cannot find the setter for attribute 'android:text' with parameter type double on android.widget.EditText.

However when I change the type to int the app builds with no issue.

Is there a way to bind a double?

like image 762
Mustafa ALMulla Avatar asked Mar 17 '16 18:03

Mustafa ALMulla


People also ask

Can we use DataBinding and ViewBinding together?

Yep, we don't use <layout> ViewBinding, because on DataBinding we must add that layout tag to generate Binding class and it's different with ViewBinding which automatically generates all layout to Binding class.

What is 2 way binding in Android?

Stay organized with collections Save and categorize content based on your preferences. The @={} notation, which importantly includes the "=" sign, receives data changes to the property and listen to user updates at the same time. // Avoids infinite loops.


1 Answers

However when I change the type to int the app builds with now issue.

Yes, but it will fail at runtime. setText(int) expects a string resource ID, not an arbitrary int.

Is there way to bind double?

android:text="@{Double.toString(BindedValue.value)}"
like image 77
CommonsWare Avatar answered Oct 14 '22 04:10

CommonsWare