将通用列表传递到< include> [英] Pass generic list into <include>
问题描述
具有两种复杂的布局,其中一种包含在另一种中. 尝试将一些自定义对象的列表传递到包含的布局中,但是构建失败并出现以下错误:
Have two complex layouts and one is included into another. Trying to pass list of some custom objects into included layout, but build fails with following error:
Error:Execution failed for task ':core:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:cannot find type element for List
file:D:\myproject-android\core\src\main\res\layout\view_header.xml
loc:101:27 - 101:42
****\ data binding error ****
view_header.xml:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="vm"
type="com.catalogz.app.ui.categories.ItemViewModel" />
</data>
<FrameLayout>
<include layout="@layout/include_title_view" bind:items="@{vm.items}/>
</FrameLayout>
...
</layout>
include_title_view.xml:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="com.catalogz.app.utils.Strings" />
<import type="com.catalogz.app.app.entities.Item" />
<import type="java.util.List"/>
<variable
name="items"
type="List<Item>" /> <!-- generic List<Item> -->
</data>
<TextView android:text="@{Strings.convert(items)}"/>
</layout>
通过删除通用定义<Item>
,有可能将这个问题从 view_header.xml 移到 include_title_view.xml ,但随后它在方法中出现在Strings.convert(items)
调用中据我所知,需要一个Item
列表,布局通过List<Object>
:
It's possible to move this problem from view_header.xml to include_title_view.xml by removing generic definition <Item>
but then it appears in the Strings.convert(items)
call as methods expects a list of Item
and layout passes List<Object>
as I understand:
Error:Execution failed for task ':core:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:cannot find method convert(java.util.List) in class com.catalogz.app.utils.Strings
file:D:\myproject-android\core\src\main\res\layout\include_title_view.xml
loc:51:32 - 51:61
****\ data binding error ****
推荐答案
我也有同样的问题,但是我找到了一种在包含的布局内设置值的替代方法.我知道这不是一个好习惯,至少它是解决包含数据绑定的include布局的设置变量的方法.
I also have the same problems, but I found an alternative for setting the value inside the included layout. I know it is not a good practice, at least it is the work around for the setting variable for the include layout with data binding.
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="vm"
type="com.catalogz.app.ui.categories.ItemViewModel" />
</data>
<FrameLayout>
<include
android:id="@+id/part1"
layout="@layout/include_title_view" />
</FrameLayout>
...
</layout>
在活动"课程中
ActivityMainBinding.part1.setItems(myItems);
希望这会有所帮助.
这篇关于将通用列表传递到< include>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!