Android: Pass different content from viewmodel to different include-tags

508 Views Asked by At

In my task app I want to use databinding for the task overview. I've grouped tasks in three categories: overdue, today and future.

My task_overview.xml defines the following viewmodel (which contains three arraylists with the different tasks)

<data>
    <variable
        name="viewModel"
        type="de.taskapp.ui.taskOverview.viewmodel.TaskOverviewViewModel" />
</data>

A few steps later I include the layout for the three task lists with the include tag like this

<include layout="@layout/layout_task_list"
                     android:id="@+id/overdued_tasks_list"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     app:layout_constraintTop_toBottomOf="@id/headline"/>

            <include layout="@layout/layout_task_list"
                     android:id="@+id/todays_tasks_list"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     app:layout_constraintTop_toBottomOf="@id/overdued_tasks_list"/>

            <include layout="@layout/layout_task_list"
                     android:id="@+id/future_tasks_list"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     app:layout_constraintTop_toBottomOf="@id/todays_tasks_list"/>

It would be awesome if I could do something like this to pass the different lists from the viewmodel to the layout_task_list.xml via the include tag

app:entries="@{viewModel.todaysTasks}" // for the todays task list case

My layout_task_list.xml looks like this

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
<data>
    <variable
        name="tasks"
        type="android.databinding.ObservableArrayList" />
</data>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/subheading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/SubheadlineWithBackground"
        tools:text="Heute"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/task_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/spacing_large"
        android:layout_marginRight="@dimen/spacing_large"
        android:nestedScrollingEnabled="false"
        app:entries="@{tasks}" //recyclerviewbinding is already there
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_constraintTop_toBottomOf="@id/subheading"/>
</android.support.constraint.ConstraintLayout>

As you may suggest...the code is not working. This error message is shown

data binding error ****msg:Cannot find the setter for attribute 'app:entries' with parameter type android.databinding.ObservableList<de.taskapp.data.entity.Task> on de.molske.einfachmachen.databinding.LayoutTaskListBinding. file:/Users/app/src/main/res/layout/layout_task_overview.xml loc:38:40 - 38:60 ****\ data binding error ****

Okay yeah its telling me that he cannot find the setter for app:entries...but isn't there a "beautiful" way to pass the three lists to the three include-layouts? I don't like the idea to copy and paste the same layout three times just to be able to pass the different lists.

Thanks in advance.

(ps.: I know that there is the possibility to solve this by java / android code but I would like to know if there is a databind way to do this ;-)

1

There are 1 best solutions below

0
On

Okay, classic me. After a few more googling I found the solution. Adding the next lines to the data-section of the included file everything works fine!

<import type="java.util.List"/>
<import type="de.molske.einfachmachen.data.entity.Task"/>
<variable name="title" type="java.lang.String"/>
<variable
    name="tasks"
    type="List&lt;Task&gt;"/>

It seems that it's important to define the exact type of the tasks variable. Because its type is a genereic &lt; and &gt; instead of < and > for defining the type.