Pass generic list into <include>

核能气质少年 提交于 2019-12-22 09:40:02

问题


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&lt;Item&gt;" /> <!-- generic List<Item> -->
    </data>
    <TextView android:text="@{Strings.convert(items)}"/>
</layout>

It's possible to move this problem from view_header.xml to include_title_view.xml by removing generic definition &lt;Item&gt; 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 ****

回答1:


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>

In Activity class

ActivityMainBinding.part1.setItems(myItems);

Hope this can be helpful.



来源:https://stackoverflow.com/questions/39293644/pass-generic-list-into-include

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!