SQLite + Android - Display Data in RecyclerView (Book Library App) | Part 3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there and welcome back to the third video of SQLite tutorial series so in the previous video we have added some data inside our database table so as you can see there are two items here title 1 and title 2 with all other data so in this video I want to add those information inside the recyclerview so here is our my database helper class which we created so here we have on create on upgrade and our latest add book method and with this method we are adding with the data inside our database table so this is our add activity we just have this onclick listener for our button and now inside this my database helper we are going to create a new method ok and then we are going to set the data in our recycler view which is inside our main activity ok so here let's create a new method so this method will return a cursor object so it's a name this is a read all data and now we're going to read off data from our database table so first let's create a string query and this Square will represent SQL SQL query so basically we are selecting all the data from our database table next we're going to create SQLite database object and we're going to use the get readable database this time next create a cursor object and set that to now so in each block we're going to check if our database is not no so that means we have some data inside our database table and then we're going to execute a row query and we're going to pass two parameters the first one is our SQL query and the second one is no ok so next what we are going to do we are going to return a cursor object and we're going to store this row query result inside our cursor and we're going to return that okay so basically our cursor will contain all the data from our database table when we return that from our read all data method here okay so this method read all data will be called inside the main activity where we are going to use the recyclerview as well so let's first initialize our my database helper class okay and let's create four different array lists so those array lists will contain strings so the first one a book ID book title book water and book pages so let's initialize this class and we're going to pass let's pass first this main activity and then we're going to initialize those array lists as well so do that for the rest of those arrays okay and the next we can create a new method here inside our main activity display data will be called and let's use a cursor object and we will to store the result from our read all data inside our cursor object so here in if block we're checking if our cursor get count is equal to zero that means there is no data we are going to display a simple toast message in that case and in else block we are going to use a cursor move to next a method and here basically we are going to read all the data from our cursor so we're going to use the get string and we're going to pass an integer here so basically the zero means we are going to read the first column which is a book ID the second cone with number one a book title and so on and we're going to call this method inside our own great method but we're going to change the name so it can make more sense store data in arrays so basically we're grabbing the results from from our read old data method and we're going to store that result in our arrays next we're going to create a custom adapter for our recycler view okay so this custom adapter will extend a recycler real class chapter and we're going to pass here a custom adapter dot my view holder so my view holder is an inner class which we are going to create now and we're going to implement three different metals for our recycle view all right so let's make this our make this inner class extends recycle view dot view holder all right and next we're going to create a new layout so this layout will be for our recyclerview row so let's name this layout my row okay and here we're going to add a card view and inside card view we want to add constraint layout so layout height for our card view should be a red content and height for our constraint layout should be wrap content as well and of course linear layout height wrap content as well it is very important to set that so let's add here a padding to 12 V P okay so row here's a little space before we add this text view so this text view first will be named the book ID text so let's add just an example number of one text size should be around maybe 30 SP or we can increase that a little bit more okay let's change the color to black okay and it's increase the text size to 40 SP I think it's it's better to do that and now we're going to add three more text views so I'm going to constrain this second the text view to our first text view and let's add one more here just below that so those two will be book title and book water so let's change the IDs of those text views okay and this one book water text let's change the text to title one this is just an example we're going to change that dynamically inside our recycler view text size for this one should be 20 SP text style bold and I think we should add a text color here as well to black okay now there is one more textview we show that and that is for book pages okay so it's strange the idea of this last text view and text 2:120 this is just an example text size to a TSP 18 SP okay and this is how our role should look like it's just a simple layout but this is just for a for tutorial purposes you don't need any more complex layout in this so in our custom adapter we want to create a constructor and we want to add some uh properties here above so let's create a context and four different array lists so next our constructor should have as a parameter as this context and four different array lists because when we initialize this class inside our main activity we want to pass all the array lists which we already created here inside main activity and then we are going to set those values to our global variables here so we can access those objects in our whole class okay book ID and one more book pages okay so let's set those access modifiers to private okay let's arrange this a little bit better okay looks nice and next inside our on create view holder we're going to inflate our row layout for our recycler view so let's use inflate method and here we're going to pass our layout and the second parameter will be view group parent and the third parameter of false and next we're going to return a new my view holder and we're going to pass view so let's store this inflator inside our view object okay and after that we're going to get inside our my view holder class here and we're going to create a four different text view objects okay and that one and next inside this constructor we're going to get the ID of those text views so for different text views and for different IDs okay and now inside on bind view holder we're going to set the text so we're going to use the holder object and we're going to set the text so here we are going to get the string from our array so here we're going to use a get method and pass a position integer so basically we are going to get all the data from our arrays okay all right so okay there is one more method we should add here and that is a get item count so we can just use a size method of any our array they are all the same okay so here in our main activity we're going to create a custom adapter object and we're going to initialize that but we're going to also use a set adapter and here as you can see custom adapter should have five different parameters and here basically we're going to use those arrays so it's very important to use to call this store data in arrays method before this custom adapter so we can grab the data in store inside our arrays and only then we can pass them into custom adapter and so inside our recycler view so now let's run our application and check it out okay as you can see we can see our item here but we can see only the second item and don't worry I know what is this all about so we cannot see actually the first item and that because we need to add a margin from the top so I'll go ahead on our main activity layout and on our recycler view we are going to add this padding-top with attribute actionbar size so now our the first item we will move down so let's run application again and as you can see now we can see all our items from our database table so there is one more thing I should add here and that's margin so let's set the margin bottom to for example 12 VP so our rules can be a little bit separated and I think instead of margin bottom I think we should add a margin top so our first element could be separated from the action bar as well so let's replace this with a margin top and now when we run an application we can see that our first item is also separated a little bit so let's add one more item here in our book library so click Add and when you click back as you can see there is a third item here so everything works perfectly fine and in the next video I'm going to show you how you can update your database items as well so thanks for watching please like this video if you find it helpful of course and see the next one
Info
Channel: Stevdza-San
Views: 137,918
Rating: undefined out of 5
Keywords: sql, sqlite, android, database, data, app, android studio, how to, guide, tutorial, sqliteopenhelper, sqlitedatabase, create, read, update, delete, crud, operations, insert, add, write, table, schema, put, query, display, in, recyclerview, recycler, view, combine
Id: VQKq9RHMS_0
Channel Id: undefined
Length: 12min 24sec (744 seconds)
Published: Thu Feb 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.