Android数据绑定@BindingConversion失败为int到字符串 [英] Android Data Binding @BindingConversion failure for int to string

查看:1311
本文介绍了Android数据绑定@BindingConversion失败为int到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


以下代码适用于浮动字符串

b

xml:

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

代码:

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

并设置:

  viewModel.number.set(3.14f); 






但是,如果我尝试相同的东西 ints to strings 我遇到了一个崩溃。

  public ObservableInt number = new ObservableInt(); 

  viewModel.number.set(42); 

我得到以下内容:

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






任何想法?
谢谢!

解决方案

android:text code> int 假定 int 是字符串资源ID。使用 android:text =@ {Integer.toString(myViewModel.number)}


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)}".

这篇关于Android数据绑定@BindingConversion失败为int到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆