Hello and welcome back guys this time we will be taking look at Data Models in RecyclerView. Data Models are used to feed information to RecyclerView items and store changes at run-time. Best way understand is by using an example consider we want create online shop. Now there are many products in shop and we decided to show all products as list and we used RecyclerView to show this list of products. Now a single product has many attributes like id, name, description, price etc. now to represent single product we will create new class as shown below
this class is called Data Model of product. Now we know what is Data Model but there are many products in shop so we will use collections like ArrayList to store all the products and feed RecyclerView. So let’s get started.
If you don’t have my previous completed project then it will be better to start from my first post of RecyclerView for your better understanding or you can download project completed up to previous post and this post below
2.Creating Data Model
Now inside project create new class which will be our item data model. This class will represent data of single item in RecyclerView. Class should look like as shown below.
2. Updating Adapter
Now in our previous Adapter we did not populated information using Data Model so we need make changes in Adapter so go to Adapter.java class. and it should look like as shown below
2.Adding some dump data
MainActivity from this activity we will feed data to our RecyclerView Adapter and Adapter will then add it in RecyclerView. MainAcivity.java class should look as shown below.
Now if you are not using images from which are online then it’s important to add Internet permission to Manifest. That’s all run application result will be as shown below.
#1 Android RecyclerView Tutorial – Introduction
#2 Android RecyclerView Tutorial – Creating Adapter
#3 Android RecyclerView Tutorial – Layout Managers
#4 Android RecyclerView Tutorial – Layout , Material Design , Decoration Part 1
#5 Android RecyclerView Tutorial – Layout , Material Design , Decoration Part 2
#6 Android RecyclerView Tutorial – Creating Data Model