Cannot bind double with Android databinding

Viewed 9461

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?

3 Answers

This is another option for converting objects to String:

android:text="@{String.valueOf(BindedValue.value)}"

This syntax also works:

android:text='@={""+item.value}'

and it can be inverted by adding a String setter:

public void setValue(String value) {
    this.setValue(Double.valueOf(value));
}
Related