I have some problem to bind a viewmodel with the u...
# android
i
I have some problem to bind a viewmodel with the ui logic to a <include> layout i have some <inlude in my layout and i want to pass my viewmodel to all inlude layout? what is the prefered solution? 🤔
g
You can include the viewModel in <include> layouts too.
i
but how? i have this
Copy code
<data>
    <import type="android.view.View" />

    <variable
        name="dataContext"
        type="canvasm.myo2.usagemon.MobileUsageNationalViewModel" />
</data>
and this
Copy code
<include
    android:id="@+id/additional_item_top"
    layout="@layout/o2theme_usagemon_additional_item_top"
    bind:dataContext="@{dataContext}"/>
and the include have this
Copy code
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="<http://schemas.android.com/apk/res-auto>">
    <data>
        <variable
            name="dataContext"
            type="canvasm.myo2.usagemon.MobileUsageNationalViewModel" />
    </data>

    <LinearLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
        xmlns:tools="<http://schemas.android.com/tools>"
        android:id="@+id/additional_info_layout_top"
        app:visibleOrGone="@{dataContext.isAdditionalTopInfo}"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="5dp"
        android:orientation="vertical">

        <TextView
            style="@style/sg.label.with_horizontal_spacing"
            android:id="@+id/um_top_additional_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:text ="@{dataContext.additionalTopInfo}"
            tools:text="Info Text hier"
            tools:visibility="visible"
            android:visibility="gone" />

</LinearLayout>
</layout>
g
bind:dataContext
try replacing this with
app:dataContext
i
Copy code
that makes no difference i got this
Copy code
D:\dev\android_b2c_app\app\build\generated\source\kapt\fl_o2Debug\canvasm\myo2\DataBinderMapperImpl.java:334: error: cannot find symbol
import canvasm.myo2.databinding.O2themeUsageMobileNationalBindingImpl;
                               ^
  symbol:   class O2themeUsageMobileNationalBindingImpl
in onCreateView in my fragment i make this
Copy code
viewDataBinding = O2themeUsageMobileNationalBinding.inflate(inflater, container, false)
    .apply { dataContext = viewModel }
g
Make sure you have
Copy code
xmlns:app="<http://schemas.android.com/apk/res-auto>"
in your parent <layout>
i
all there
@George Mount or @yigit can u help
y
sorry saw this late but the include looks fine, probably there is some other error that breaks
O2themeUsageMobileNationalBindingImpl
code generation hence the other error is received
there should be something else about O2themeUsageMobileNationalBindingImpl in the error logs