简体   繁体   中英

Android Data Binding @BindingConversion failure for int to string

Ran into a mysterious problem when trying to make a @BindingConversion for int to string.
The following code works for floats to strings :

xml:

...
<variable
        name="myViewModel"
        type="... .SomeModel" />
...
<TextView
            style="@style/StyleStuff"
            android:text="@{myViewModel.number}" />

code:

public class SomeModel {
    public ObservableFloat number = new ObservableFloat();
}

and setting:

viewModel.number.set(3.14f);

But if I try the same thing for ints to strings I get a crash.

 public ObservableInt number = new ObservableInt();

with

viewModel.number.set(42);

I get the following:

FATAL EXCEPTION: main
Process: ...myapplication, PID: 14311
android.content.res.Resources$NotFoundException: String resource ID #0xfa0
    at android.content.res.Resources.getText(Resources.java:1123)
    at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
    at android.widget.TextView.setText(TextView.java:4816)
    at ...executeBindings(ActivityAdaptersBinding.java:336)
    at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:355)

Any ideas? Thanks!

android:text with an int assumes that the int is a string resource ID. Use android:text="@{Integer.toString(myViewModel.number)}" .

You will also need to import the Integer class: (no longer needed)

Beware , In latest DataBinding (2019), It does NOT require importing Integer nor String or you will get this error:

****/ data binding error ****msg:Missing import expression although it is registered file

official doc says : java.lang.* is imported automatically.

Just go either

android:text="@{Integer.toString(myViewModel.number)}" or

android:text="@{String.valueOf(myViewModel.number)}"

directly.

Convert int to string for set in TextView like below:

android:text="@{String.valueOf(myViewModel.number)}"

Also, String class must be imported by the layout:

<layout>
   <data>
      <import type="java.lang.String" />
   </data>

   …

</layout>

Simplest solution, may be it would help someone.

android:text="@{`` + model.intValue}"

This can be used in two-way binding for EditText also. Where users input is set as Integer value in model, and shown as String .

android:text="@={`` + model.intValue}"

See this answer also.

This worked for me

  <TextView
        android:id="@+id/number"
        android:text='@={Converter.convertIntToString(myViewModel.number)}'

        />

Converter class with inverse method

public class Converter {


public static int convertStringToInt(String text) {
   return Integer.parseInt(text);
}

@InverseMethod(value="convertStringToInt")
public static String convertIntToString(int value) {
    return Integer.toString(value);
}}

It's too late but i update it for newbies. You can parse to int in xml by using:

Integer.parseInt(string)

or to String by:

String.valueOf(string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM