-1
<EditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/cow_no_of_child_edittext"
    android:inputType="number"
    android:text="@={String.valueOf(cow.noOfChild)}" <- This atribute
    android:hint="@string/no_of_child"
    android:textSize="@dimen/text_size_22sp"/>

Hi, I am facing problem while working in data binding in android. EditText is taking string value successfully. But It is not working for Integer value. In Android Monitor Log I am getting these exception messages:

Error:(165, 34) The expression java.lang.String.valueOf(cowNoOfChild) cannot cannot be inverted: There is no inverse for method valueOf, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions Error:(165, 34) The expression java.lang.String.valueOf(cowNoOfChild) cannot cannot be inverted: There is no inverse for method valueOf, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions Error:(165, 34) The expression java.lang.String.valueOf(cowNoOfChild) cannot cannot be inverted: There is no inverse for method valueOf, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions

Please provide me solution.

Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
Rajendra Verma
  • 439
  • 5
  • 16
  • `android:text` can only take a value such as `@string/something` which is equivallent to `R.string.something` and must be defined in `strings.xml` – Shark May 19 '17 at 09:51
  • I know , but I am talking in context of data-binding in android. please see link for more information on topic. https://developer.android.com/topic/libraries/data-binding/index.html#build_environment. Just cross check section Expression Language. – Rajendra Verma May 19 '17 at 10:03
  • Refer [Android two way binding with Integer type causes databinding does not exist](http://stackoverflow.com/questions/38998222/android-two-way-binding-with-integer-type-causes-databinding-does-not-exist) – Ravi May 19 '17 at 10:11

1 Answers1

1

You need to declare @BindingAdapter to set String value to android:text property and @InverseBindingAdapter to return value back:

public class BindingUtils {

    @BindingAdapter("android:text")
    public static void setText(EditText view, String value) {
        view.setText(value);
    }

    @InverseBindingAdapter(attribute = "android:text")
    public static String getText(EditText view) {
        return view.getText().toString();
    }
}

See also another example for float usage

Community
  • 1
  • 1
Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60