0

I'm using Two Adapter with data Binding implements and One Specific item layout for each adapter,However I used One Data Model Type for Two of them...is this really possible? is it Okay for standard coding? Because i get error in one of generated Data Binding Classes

My Main Question Is: is it Standard in pattern of DataBinding or even its a good practice or not?And Why?

ERROR : error: cannot find symbol import packageName.ItemCheckStepsBindingImpl;

My Adapter Item Layout :

    <?xml version="1.0" encoding="utf-8"?>
    <layout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <data>
            <variable
                name="checkSteps"
                type="packageName.data.model.Steps"/>//in this Line i'm using again in another Item layout
        </data>

        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@color/white"
            app:cardElevation="8dp"
            style="@style/MyCard"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="2">



                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/user_confirm"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left|center_vertical"
                    android:layout_margin="@dimen/inner_margin"
                    android:layout_weight=".1"
                    app:srcCompat="@drawable/ic_check_green_24dp"
                    android:visibility="invisible"
                    />

                <LinearLayout
                    android:id="@+id/container"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="@dimen/inner_margin"
                    android:orientation="vertical"
                    android:layout_gravity="right"
                    android:layout_weight="1.8"
                    >


                    <androidx.appcompat.widget.AppCompatTextView
                        android:id="@+id/title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:text='@{steps.title}'
                        style="@style/HeadTextView"
                        android:layout_margin="@dimen/inner_margin"
                        />


                    <androidx.appcompat.widget.AppCompatTextView
                        android:id="@+id/description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:text='@{steps.description}'
                        style="@style/BodyTextView"
                        android:layout_margin="@dimen/inner_margin"
                        />


                </LinearLayout>

            </LinearLayout>

        </com.google.android.material.card.MaterialCardView>

</layout>

My Second Adapter Item :

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="steps"
            type="com.isatelco.diettrainer.data.model.Steps"/>
    </data>

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/white"
        app:cardElevation="8dp"
        style="@style/MyCard"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="2">


            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/deleteStep"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight=".1"
                android:layout_gravity="center_vertical"
                app:srcCompat="@drawable/ic_clear_red_24dp"
                />

            <LinearLayout
                android:id="@+id/container"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/inner_margin"
                android:orientation="vertical"
                android:layout_gravity="right"
                android:layout_weight="1.8"
                >


                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text='@{steps.title}'
                    style="@style/HeadTextView"
                    android:layout_margin="@dimen/inner_margin"
                    />


                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text='@{steps.description}'
                    style="@style/BodyTextView"
                    android:layout_margin="@dimen/inner_margin"
                    />


            </LinearLayout>

        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>


</layout>
HamidSayyah
  • 423
  • 3
  • 19

2 Answers2

0
<data>

    <variable
        name="name"
        type="com.example.Item"/>
</data>

/in this Line i'm using again in another Item layout

This is nothing else a normal variable, same like in Java class. You can declare a variable as many times you want.

error: cannot find symbol import packageName.ItemCheckStepsBindingImpl;

Correct import should be

<data>
    <variable
        name="checkSteps"
        type="com.yourpackage.data.model.Steps"/>//in this Line i'm using again in another Item layout
</data>

If there are chances of mistake, then just type Steps in type, then accept suggestion.

If problem is not solved, then also check this answer to make sure you are not doing any of this answer mistakes.

Update

Make sure you are importing yourpackage.databinding.ItemCheckStepsBinding NOT packageName.ItemCheckStepsBindingImpl in your adapter.

Update

you can not have two items with two different name in data variable type :

You are wrong here. you can define any number of variable with many names.

layout_one.xml

<variable
    name="itemOne"
    type="com.example.Item"/>

In java class

  binding.setItemOne();

layout_two.xml

<variable
    name="itemTwo"
    type="com.example.Item"/>

In java class

  binding.setItemTwo();

layout_three.xml

<variable
    name="itemOne"
    type="com.example.Item"/>

<variable
    name="itemTwo"
    type="com.example.Item"/>

In java class

  binding.setItemOne();      
  binding.setItemTwo();

All the above cases will work.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • i checked it out and there was no problem with package name...my package and Model class was okay.thanks for your help – HamidSayyah Oct 03 '18 at 10:06
  • That is Weird Because when i change name of two items the same the Data Binding Error Gone...But i will test this in a few hours later and answer you back...thanks ;) – HamidSayyah Oct 03 '18 at 10:53
0

okay.i find out where is the problem.you can not have two items with two different name in data variable type with one Data model:

<data>
    <variable
        name="name"
        type="com.example.Item"/>
</data>

i think its a unknown aspect of data binding that you can not give two different name in two items variable "name" with one model "type" so the solution is to give one specific name to two items.

HamidSayyah
  • 423
  • 3
  • 19