Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

gabriel wambua
gabriel wambua
5,061 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

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
5,061 Points

Thanks Lauren this really helped

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