2017-08-02 65 views
2

試圖綁定雙值使用字符串資源以查看。的Android結合雙值,以查看弦使用資源

嘗試1:

layout.xml

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

string.xml

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

嘗試2:

layout.xml

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

string.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?

+1

當你回答問題時,你有困難嗎?他們看起來像他們回答你的問題,你的嘗試不使用它們。 –

+0

是的,我曾嘗試自提鏈接這兩個解決方案。 – Puni

+0

這應該工作:'機器人:文本= 「@ {@串/ user_popularity(user.popularity)}」'和字符串資源'<字符串名稱= 「user_popularity」>流行度:%1 $ F'假設用戶。流行是一種「雙重」或「浮動」。你在看什麼?是編譯時錯誤,運行時異常還是格式錯誤?這可能是因爲你還沒有使用'binding.setUser()'分配一個用戶。 –

回答

1

其實這是很簡單的,是我不好,我做了錯誤的方式。 這裏的解決方案。

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