Android Android Lists and Adapters Adapters and ViewHolders Adapter Class & ViewHolder Sub-Class

gabriel wambua
gabriel wambua
356 Points

Ok I am totally lost on theData variables

Ok I am totally lost on the following Data variables and what determines what variables to use as Name and Type variable

and two How do we create the DataBinding name with the Binding preffix within the view holder.... i completely did not understand that bit... I tried doing it in my project but my LayoutNameBinding name is not recognized as anything.

Please help

2 Answers

Lauren Moineau
Lauren Moineau
6,646 Points

As you know, each binding variable inside the <data> element has a name and a type.

Here, the type is the path to the model class that holds the data you want to bind. For example, let's take an app called BookInventory, with a package name: com.something.bookinventory. For that app, a Book object was created, with fields like title, author, date, etc.., so that there's a model class called Book and we want to bind that data. This app follows a clean architecture pattern (MVP or MVVM) and stores the Book class in a package named "model". So, when you look at the Android project tree in Android Studio, in the java folder, you will have: java > com.something.bookinventory > model > Book. The binding variable "type" will therefore be com.something.bookinventory.model.Book (= the path to the Book class).

The name is just a name of your choice, to refer to that variable in the layout file. Common practice is to use the name of the model class where the data you want to access is, in lowercase or camel case. Here, it would be book.

So for that example, the <data> element would be:

<data>
    <variable
       name="book"
       type="com.something.bookinventory.model.Book"/>
</data>

As for the ViewHolder, the generated binding class takes its name from the item's layout name. So in our app example, if the item's layout is called book_list_item.xml, the generated biding class will be BookListItemBinding (= the XML layout name, minus the underscores, in upper camel case, plus the word "Binding")

Hope this helps.

gabriel wambua
gabriel wambua
356 Points

Thanks Lauren this really helped

Lauren Moineau
Lauren Moineau
6,646 Points

You're welcome Gabriel. Glad I could help :)