【问题标题】:Android binding double value to view using string resourcesAndroid绑定双值以查看使用字符串资源
【发布时间】:2023-03-27 22:46:01
【问题描述】:

尝试使用字符串资源将双精度值绑定到视图。

尝试 1:

layout.xml

android:text="@{@string/user_popularity(Double.toString(user.popularity))}"

字符串.xml

<string name="user_popularity">Popularity: %s</string>

尝试 2:

layout.xml

android:text="@{String.format(@string/user_popularity, user.popularity)}"

字符串.xml

<string name="user_popularity">Popularity: %.1f</string>

错误:

Error:(167, 107) error: double cannot be dereferenced

这里有一些类似的问题

  1. How do I format a double using Android view data bindings?

  2. How do I use databinding to combine a string from resources with a dynamic variable in XML?

【问题讨论】:

  • 你在听答案的时候有困难吗?它们看起来像是在回答您的问题,而您的尝试并未使用它们。
  • 是的,我已经尝试了提及链接中的两种解决方案。
  • 这应该可以工作:android:text="@{@string/user_popularity(user.popularity)}" 和字符串资源 &lt;string name="user_popularity"&gt;Popularity: %1$f&lt;/string&gt; 假设 user.popularity 是 doublefloat。你在看什么?是编译时错误、运行时异常还是格式错误?可能是您没有使用binding.setUser() 分配用户。

标签: android data-binding android-databinding


【解决方案1】:

其实很简单,可惜我做错了。 这是解决方案。

<string name="user_popularity">Popularity: %.1f</string>
android:text="@{@string/user_popularity(user.popularity)}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    相关资源
    最近更新 更多