简体   繁体   English

为什么等于总是在布局 xml(数据绑定)中返回 false?

[英]Why equals always return false in layout xml (databinding)?

I'm working on this project https://developer.android.google.cn/codelabs/basic-android-kotlin-training-project-lunch-tray , and the final project is very similar to https://github.com/ErickPimentel/LunchTray , but I add android:checked to RadioButton , for example, @{viewModel.accompaniment.name.equals(@string/bread)} (I put all hard coded strings in string.xml ).我正在做这个项目https://developer.android.google.cn/codelabs/basic-android-kotlin-training-project-lunch-tray ,最终的项目非常类似于Z5E056C500A1C4BADE5/github.com /ErickPimentel/LunchTray ,但我将android:checked添加到RadioButton ,例如@{viewModel.accompaniment.name.equals(@string/bread)} (我将所有硬编码字符串放入string.xml )。 But it does not work fine, it can't checked any radio button.但它不能正常工作,它不能检查任何单选按钮。

I check your AccompanimentMenuFragment android:checked="@{viewModel.accompaniment.name.equals(@string/pickles)}"我检查了你的 AccompanimentMenuFragment android:checked="@{viewModel.accompaniment.name.equals(@string/pickles)}"

Here viewModel.accompaniment.name has value "Pickled Veggies" and in string "pickles".这里 viewModel.accompaniment.name 的值是“Pickled Veggies”和字符串“pickles”。

viewModel.setAccompaniment(@string/pickles)}" is setting value from your DataSource which has name "Pickled Veggies". viewModel.setAccompaniment(@string/pickles)}" 是从名称为“Pickled Veggies”的数据源设置值。

_accompaniment.value = menuItems[accompaniment] in this line menuItems[accompaniment] is getting value from DataSource which has此行中的 _accompaniment.value = menuItems[accompaniment] menuItems[accompaniment] 正在从具有

"pickles" to MenuItem( name = "Pickled Veggies", description = "Pickled cucumbers and carrots, made in house", price = 0.50, type = ItemType.ACCOMPANIMENT ) “泡菜”到 MenuItem(名称 =“腌制蔬菜”,描述 =“腌黄瓜和胡萝卜,自制”,价格 = 0.50,类型 = ItemType.ACCOMPANIMENT)

Hence you "Pickled Veggies" is getting compared with "pickles" which gives false result.因此,您将“腌制蔬菜”与“泡菜”进行比较,得出错误的结果。 Same issue is with other fragments其他片段也有同样的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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