SQLite Database Tutorial Android Studio | Note List View App with Persistent Data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys my name's cal and in today's java  and android studio tutorial we're going to   be creating a basic sqlite database so that we  can save data in our app and make it persistent   in this example we're going to be creating  a note-taking app using a list view   but the principles of creating a  row editing a row or deleting a row   should be the same and could be used throughout  any android application today's tutorial is a   little bit of a longer one so i'll be sure  to leave timestamps in the description below   in the first part of the tutorial we're going to  get our note list view up and running so we're   going to have it so that you can create a new note  and it's just going to save to memory we're just   going to keep it in an array list in the next  part of the tutorial we're going to create our   sqlite open helper class which is going to manage  all of the sort of translation from our java code   into an sqlite database so we're going to  be able to add a note to the database we're   going to be able to edit a note in the database  and as well set a flag deleted in the database   creating a new android studio project i'm going to  call this one sqlite android studio tutorial our   programming language is java and just finished  to complete the android studio project and then   the first thing we're going to do is head into our  activity main xml we're going to change the layout   to a linear layout we're going to get rid of the  text view and add in a toolbar toolbar is going   to have a width of match parent and a height  of wrap content the min height so the minimum   height is going to be of action bar size and the  theme is going to be action bar theme so you just   need to put in a question mark and then type in  both of those things next what we're going to do   is add in some colors i'm going to create this  dark gray color as well as a red color obviously   you can choose whichever colors you like next  what we're going to do is delete the night theme   dot xml and then we're going to change the parent  theme to material components light now action bar   we're going to change the primary color to dark  gray variant to black and our on primary to   dark gray and then just remove all of the other  color stuff cool so now we can head back into our   toolbar we can apply that dark gray to our toolbar  we're going to add in a text view to our toolbar   we're going to call it notes text size of 30sp  as well as a text color of white we're going to   set the layout gravity to center as well as  giving the text style bold and then we're also   going to create inside our toolbar we're going to  create a button this is going to be to add a new   note so i'm just giving it the text of the plus  symbol as well i'm going to copy and paste down   the text style and size and that sort of thing  and then just change the layout gravity to end   so it sits right at the end and then we're going  to create an on click i'm just calling it new note   and then we're going to create that inside our  main activity and then below our toolbar we're   going to create our list view so just giving it  a height and width of match parent as well as   i'm going to give it a layout margin of 3dp  as well as that divider height which is 3dp   and then set our divider color to dark gray so  just saying divider dark gray we're going to give   our list view an id so just calling it note list  view and that's all looking pretty good so we can   create a new activity i'm going to call this one  note detail activity this activity is going to be   used for editing as well as creating a new note  and then we're just going to head back into our   main activity xml we're going to copy and paste  the toolbar because it's going to be mostly the   same we're just going to change the text view to  detail and the buttons text to save we're going   to give the background color this white as well as  the changing the text color to dark gray as well   we're going to change the on click method and just  create that inside our note detail activity next   we're going to create a text view we're going  to copy and paste down most of the styling from   our toolbar text view as well we're going to  i've just realised i haven't actually made this   layout a linear layout so we're just going to do  that i'm going to change that to a linear layout   and set the orientation vertical and now that  should be a little bit happier we're going to   change the text color to dark gray as well as  making the text size a little bit smaller and   changing the text to title we're going to copy  and paste that down because we're going to have   a the ability to enter a title and a description  so we're just going to copy and paste that down   and change the text next we're going to enter in  an edit text so we're going to give it a width   of 300dp as well as a height of wrap content i'm  going to copy and paste most of our styling again   and then just giving our edit text max lines of  one and then copy and pasting that down and our   description we're going to allow them to have  a little bit more space we're going to make our   minimum lines three next we're going to give both  of our edit texts an id so i'm just calling the   first one description edit text and the top one  calling it title edit text cool so that's looking   pretty good next thing what we're going to do  is create our cell so i'm going to create a new   layout resource file i'm calling it note cell  as well we're going to create the java class   to link to it so we're just calling that one note  adapter inside our note cell we're going to give   our linear layout to have some padding of 15dp as  well as giving it a fixed height so 100dp and then   we're just going to put in a couple of text views  wrap content on both the width and the height   giving the first one text of title as well as  giving our text style of bold minimum lines 1   text color dark gray and text size 25sp and now  we can copy and paste that one down we're going   to change the text to description we're going to  get make it less bold because it's probably a bit   less important what the description is we're just  going to have it a little bit smaller as well and   then we're going to give it a little bit of margin  to the top next we're going to give both of our   textviews an id and that pretty much wraps up all  of our xml now all we have to do is make it work   i'm just going to say file new java class calling  it note and our note class is going to have   an int id string title string description as  well as a date deleted and then i'm just going   to right click and generate getters and setters  for all four of those fields as well as i'm going   to create two constructors one with which takes  all four fields and then another one which takes   them all except for deleted and then on this one  we're going to make it so that deleted is equal   to no you don't actually have to put in this line  deleted would be null but it's just to illustrate   that that's what it will be when you use that note  constructor and then we're going to head back into   our note adapter class we're going to say extends  array adapter of type note and then we're going   to create a constructor i'm just calling it note  adapter it takes in a context as well as a list   of notes and we're just going to call this notes  then we're just going to call super we're going   to give it our context resource of zero as well as  our note list next we're going to override the get   view function so i'm just starting to type in get  view and then enter on the first function that you   see so you can see get view has position convert  view and parent and then we're going to say note   is equal to get item at position and then if  convert view is equal to null we're going to   initialise our convert view by saying it's equal  to layout inflator dot from get context inflate   R dot id layout note cell and then we're also  going to pass it in parent and attach to root is   equal to false next we're going to declare a text  view i'm going to call it title and then we're   going to find that text view from our convert  view find view by id and we're going to give it   our cell title and our cell text as well as our  cell description so both of those text views from   our cell text view and then we're just going  to say title dot set text and giving it a note   dot get title and the same thing for description  and then finally we're just going to return our   convert view inside our main activity now we're  going to declare our list view just calling it   note list view and then we're going to create a  function calling it init widgets and we're just   going to find our note list view via its id next  we're going to create another function we'd call   it set note adapter and i'm just going to put  this below our init widgets method and inside   our set note adapter i'm just going to say note  adapter is equal to new note adapter giving our   application context so get application context  as well we haven't actually created our list view   so i'm going to create that now so i'm just going  to head into our note.java i'm going to declare a   public static array list of notes calling it note  array list and now we can get our note array list   from our note class so just saying note dot note  array list because it's public and static that   means we can access it from outside of its class  and then we're just going to say no list view dot   set adapter giving it our note adapter cool so  next in our new note function we're just going   to declare an intent i'm going to call it new note  intent giving it our context of this as well as   our note detail activity class we're just going  to call start activity giving it our intent next   inside our note detail activity we're going to  declare two edit texts the first one is title edit   text and the second one is description we're going  to declare another function inside here calling it   init widgets and we're just going to find both of  those edit text via their ids next inside our save   note function we're going to declare a string  calling it title which is just the text of the   title edit text as well as the description of the  text and our id is going to be equal to our note   array list size and below that we're just going  to create a new note object just calling it note   is equal to new note we're going to pass it our id  title and description and so obviously our deleted   is going to be null because we're using that  constructor we're going to get our note array list   and add our new note to our list and then we're  just going to say finish to finish that activity   and go back to the one that we were previously  on which is our list view cool and if we build   and run this now you can see we can add a new note  and hit save and it'll pop it back into our note   list um obviously the data isn't persistent yet we  haven't actually touched any sql and what are we   eight minutes into the tutorial maybe even more  but we've got something we can now work with so   what i'm going to do next is right click create a  new java class i'm going to call it sqlite manager   making the class public and our sqlite manager  extends sqlite open helper and it's going to give   us an error message saying we don't have the on  create and on upgrade functions and i'm just going   to create those as well as create a constructor  our constructor is going to be a little bit   smaller than what we've got here i'm actually  going to remove all this nullable stuff and   just have our sqlite manager constructor passing  context and right at the top i'm going to declare   a private static sqlite manager just calling it  sql lite manager as well we're going to declare   a private static final string calling it database  name you can call this whatever you like we just   need to have a string and it's much easier to keep  it consistent like this we're going to copy and   paste that down a few times changing the next one  to database version is equal to one because this   is the start of the database table name is note  again that can be whatever you like and we're   going to create a string calling it counter again  this can all be pretty much whatever you like   but you just want to have a string that you can  reuse and then i'm going to create the fields for   id title description and delete it and then i'm  also going to declare a date format this is going   to come in handy when we are managing our deleted  we're actually going to store it rather than as a   date we're going to store it as a string and then  we're just going to use the same date format to   convert it from a string into a date back into a  date cool so now in our constructor we can pass in   out to our super we're going to pass in our  context our database name factories null and   our database version and then below that we're  going to create another public static method and   it's going to be called instance of database and  it's going to return our sqlite manager it's also   going to take context basically all it does is  if sqlite manager is equal to null the one that's   living inside our class we're going to initialise  it and create the new sqlite manager otherwise if   it's already been created we're just going to  return the sqlite manager cool so inside of our   on create i'm going to declare a string builder  i'm calling it sql and i'm just going to create   basically a very simple sql statement which is  going to create a table we're going to give it   our table name i'm going to give it a primary key  which is our counter and then we're going to give   it all of our four fields so our first one is  an id field which is of type int and our second   one is a title field which is of type text and  same thing for our description and our deleted   field they're all going to be stored in our sql  databases as text so basically on create when   there isn't a database it's going to run this  statement and it's going to create the table and   then we're just going to say sqlite database dot  execute sql and we're just going to take our sql   string builder and put it to a string and then  inside our one upgrade i'm just going to give you   a quick example let's say if you wanted to add a  new field all you need to do is change the version   number at the top so 2 and then you want to create  a switch in here which says if the old version is   one you are going to alter the table and add a  column like this and then if you wanted to do   that again you could just say well if the version  number is two and so on and so forth but we don't   need to do this this is just an early example  so i'm going to comment out that and we're going   to create a method we're going to call add note  to database which is going to take a note we're   going to say sqlite database is equal to this get  writable database and then we're going to declare   content values we're just going to call it content  values is equal to new content values and then   we're going to call our content values put and  put takes two inputs and the first thing we give   it is our string which is our field and then the  value that corresponds so note for id we're going   to say id field and then we're going to say note  dot get id and i'm going to do the same thing for   title description and deleted for our deleted  one we actually want to create a function calling   it get string from date and i'm just going to  create that below and basically all we need to do   is have this function take a date and return  a string and inside here we're going to say if   the date is equal to null return null otherwise  date format which is the one we created at the   top format date and we're just going to return  that while we're here we're going to create the   opposite of that so we're going to take a date  and turn it back into a string all we need to   do is return a date format pass to string except  that needs to be in a try catch statement as well   we're going to make it check for our null pointer  exception and then we're just going to return null   if it finds either of those exceptions now we can  head back up to our add note to database function   and we're just going to say sqlite database insert  i'm going to give it our table name null and our   content values the next function we want to create  or the next method we want to create is going to   be repopulating our memory when we load up for the  first time so that's what we're going to do we're   just going to call it populate note list array  we're just going to say sqlite database is equal   to this get readable database and we're going  to create a cursor calling it result is equal to   sqlite database raw query and we're just going  to say select all from our table name and then   passing our selection arguments as null we're just  going to pull every single item inside our table   and put it into our cursor result and then we're  going to say if our result count is not equal to   zero we're going to create a while statement we're  going to say well result move on to the next to go   through each one of our results we're going to say  our id is equal to our result get int at position   1 and then we're going to say because we know our  title is at position 2 we're going to say string   title is equal to result get string at position  2 same thing for description as well as deleted   but now with our string deleted we actually need  to turn that back into a date we're just going   to declare a date below it calling it deleted and  then we're going to call our get date from string   method giving it our string date once we've got  all four of our fields out of our database we're   just going to declare a new note and we're going  to pass it through all four of our fields so id   title description and deleted and then we're just  going to add that to our note list array cool the   next function we want to create is update note  in database so this is when we're editing a note   we're just going to pass in a note and we want it  to repopulate our database with our edited note so   that's what it's going to pass in as a note so  it's very similar to our add note we're going to   create content values we're going to put from each  one of our fields getting it from our note we're   going to get the string from the date and the  title and so on and so forth but then we're going   to call sqlite database update we're going to give  it our table name our content values and then it   also takes a where clause and our where clause  is going to be where our id field is equal to   we're going to create a new string array string  value of our note id so anywhere that that matches   our id we're going to update in the sql database  cool so i'm just going to head up and fix a typo   here we need to add in a space between the deleted  field and text what we can do now is head into our   main activity and we're going to create a function  calling it load from database to memory and we're   going to declare an sqlite manager we're going to  call our instance of database and passing it in a   context of this we're going to call that populate  note list array that we created so when the user   first opens the app it's going to repopulate from  our database into our memory note list array cool   and inside our note detail activity when we save  a note we not only do we want to save it to our   note list array in our memory we also want to  save it to the database so i'm just going to   declare our sqlite manager getting instance of  the database again passing it in context of this   sqlite manager add note to database and passing  it in our new note now if we build and run this   you can see that when we add a new note i'm just  going to call this one by clock and we're going   to create another one i'm going to call this  test if we close the app and then we reopen it   we should be running our populate note list and  which it has and you can see that all of the data   is persistent next we want to add in the ability  to edit a note so i'm just going to create another   function inside our main activity i'm going to  call it set on click listener and i'm going to   put that below our note adapter we're just going  to say no list view set on item click listener   and then we're going to say new adapter view on  item click listener auto complete that function   and we're going to have an on item click  method and then we're just going to say no   selected note is equal to note list view get  item at position and then passing it in our   position which is actually this integer here  so i'm just going to rename that to position   and i'm going to say intent edit note intent is  equal to new intent giving it application context   as well as our note detail class so very similar  to our new note intent except we're going to do   edit note intent put extra and then we need to  give it a string so i'm going to create a public   static string i'm going to call it note edit  extra and this can literally be whatever you   like but you just want to make sure it's the same  as when we pull it in our note detail activity so   i'm just going to say no dot note edit extra as  well giving it our selected note dot get id start   activity passing it out edit note intent cool so  then inside our note detail activity we're just   going to create a method called check for edit  note inside this method we're going to declare   an intent we're going to call it previous intent  which is equal to get intent and then we're going   to clear an int we're going to call it past note  id is equal to previous intent dot get int extra   and we're going to call that same string so note  edit extra and the default value is going to be   negative 1. and then we're going to say selected  note is equal to note dot get note for id which we   haven't created this method yet i'm going to click  on that error message and create that inside our   note class and inside here we just want to say for  note in our note array list if our note get id is   equal to our pass note return the note that you  found otherwise if you've gone through the whole   list and you haven't found a note matching the  id return now and then we're going to say if our   selected note is not equal to null so we've found  a selected note meaning we're in the edit note   we're going to say title edit text set text and  giving it our title and we're going to do the same   thing for description so just repopulating the  edit text with whatever value that has already   previously been saved and then inside our save  note we're going to create an if selected note   is equal to null we're going to do what we've  already been doing which is creating a new note   and adding it to our database but if selected  note isn't equal to null meaning we're in edit   mode we're going to say selected note that title  giving it our title same as description call our   sqlite manager and update our note in our database  which we've already created that method cool so   let's head in and test this and see if we can edit  a note and close our app and then reopen it and   see that the data is being updated so that'll look  good next thing what we're going to do is add in a   delete so i'm just going to copy and paste down  this button from our save changing the text to   delete set the gravity to center the margin to the  top to be 20dp as well as changing the text color   to be red and change the on click to delete note  we're going to create that method inside our note   detail activity and then we're also going to give  our button an id i'm just going to call it deleted   note button inside our delete note we're just  going to say selected note set deleted and we're   just going to call new date so giving it exactly  the time that it was deleted and then we're going   to copy and paste down our sqlite manager instance  of database as well calling our update note in   database passing it in our selected note and then  just call finish so that when we delete the note   it takes us back to our list view activity right  at the top we're going to declare a button we're   going to call it delete button and we're just  going to find that by its id in our init widgets   function then in our check for edit note and where  we have if selected note is not equal to no we're   just going to create an else statement and hide  our delete button if we're in new note mode inside   our main activity we're just going to override the  on resume function and we're just going to call   set note adapter so that it reloads every time the  activity is resumed and then inside our note we're   going to create another array list or a function  that returns an array list of notes we're going to   call this non-deleted notes because obviously  we've only set the flag deleted we haven't   actually deleted any notes so when we populate  our list we want to populate with only non-deleted   notes we're just going to go through our note  array list and if we find a note that's deleted is   equal to null we can add that to our non-deleted  note list if it does have a date there then   obviously that's not going to get added to our new  array list now when we set our note adapter we're   just going to get only notes that aren't deleted  cool and if we build and run this one last time   you can see that we can create a note i'm going to  call it note to delete and then i'm going to hit   delete and you can see that when we return here  it's gone we're also going to delete this test   modified note there we have it it's very basic how  to make data persistent in android studio using   java if you enjoyed the tutorial give it a like  otherwise i'll catch you guys in the next tutorial
Info
Channel: Code With Cal
Views: 5,366
Rating: 4.8360658 out of 5
Keywords: java, tutorial, programming, coding, example, android, studio, program, code, app, development, developer, software, indie, how, to, basic, simple, 2021, education, build, create, apps, easy, follow, beginner, friendly, make, learn, data, base, database, sql, sqlite, sql lite, list, view, listview, note, note app, persistent, taking, sql database, notes, learning, idea, helper, sqlite open helper, edit data, save data, how to save data, in android studio, how to, row, modify, alter, select, from, where, clause, java class, cell, adapter
Id: 4k1ZMpO9Zn0
Channel Id: undefined
Length: 18min 37sec (1117 seconds)
Published: Thu Apr 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.