Saving data with SQLite and adding it to a ListView

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody welcome to another Android studio tutorial my name is Mitch Tavian and in this tutorial I'm going to show you how to populate a list view and store items of a ListView into an SQL database or store items and then populate a list view with those items there's a couple ways you can do this you can use to shared preferences but but the best way I think is to do use SQL so you can reference reference the list items that exist so do a little demo over here and say add that then click on view entries and I entered those already so I don't know the others names like so check throw that in there and that's yeah so we'll do is create a new project start it's what we call Oh save it somewhere somewhere else try that blank activity and you can do it there you go okay so the first thing we'll do is design the layout for entering items so it'll be this layout right here and I guess let's put a margin on top to 20 DP share and that one okay so you lay it so the first thing we'll do is let me actually make the database helper actually I'll just make it a layout right now it's easy so this is just going to be your view contents I guess and this one is just going to be a list video I just throw a list view in there and it should be called this view yeah okay let's dump easy now we're going to create our database helper so I I know when I was learning first learning how to use a database that I yourselfer seemed pretty intimidating so if you're just learning just just straight up copy this and don't don't worry about too much else because you'll eventually understand how it works it's not very complicated but it might seem complicated right now okay I think that's all the kind of stuff that you need to have in here so now now we can create the methods that we're going to use in our in our other classes so we're going to have an ADD data obviously because we need to add data to the database so I'll make it a boolean so that we can we can check if everything added correctly xq all right database dB right up with database and then use content values to add data put and we're going to put we're going to add column add the data to column two because that's where our item is being stored so like if I wanted to add more columns it's very simple I would just go create another column called column three and call this something else there's a few other things you need to do too but like generally that's it insert is a hooked in command takes your database name content values so here's what we check if it gets inserted correctly so if result is because this this will return a click on this it will return a negative one if if it fails to enter and it will return if it returns anything else that means it was entered correctly basically so if it returns negative one or if it yeah if the insert returns negative one we're going to return false and I'll show you how we're going to use that in our other class later to check if it was entered correctly of so and otherwise if it was I think correctly it's going to return true well so and if you want to if you want to learn more about SQL databases check out my other my other tutorial on SQL Lite for Android because I go over everything a lot in a lot more detail than what I'm doing right now we need a cursor here for getting all this contents yeah so this method right here that I'm writing right now is for getting the contents of the database so use a select query select star means select all from and you put the database in here so select all from database on a side-table sorry select all from table then you return okay so that's it for the for the for the database helper so like I said if if you're if you're kind of new to android don't worry about the database help or just straight-up copy this and if you want to learn how to enter more stuff into database like have more columns watch my other tutorial I'll put I'll put a little link on here and yeah so slow it just make sure I got everything here I think it's good so yeah and now I think we can go into our main activity and we can start writing the code down B's helper call up my database I'm going to make it a button two buttons actually button add and we'll call button view and we have an edit text we'll just call it a text so we have one so that it text equals from you okay so there's not it's not going to be much to this we just basically a couple on click on click listeners so we'll do oops button add dot on someone so I'm click listener you go new capital o and it should pop up right there just click enter and it'll it'll build it for you so when you're streaming we'll call it new entry pulse and get the text if whoops a text so this is just going to be two check check to make sure if they actually wrote something down so if the length does not equal zero then we're going to go with the writer methods first so we're going to I'm going to write a method down there called add add data then you just pass the string to it and then at the end of that we want to set this text to blank again else toast so this is if if they hit add and that this will this will come up if they hit add and they didn't actually write me in the field because it will mean the length equals zero okay so create the ad data of like whoops and we're going to need to give it a string let's call it your entry win so we use a boolean here because in the database helper that's what we use so we need to use the same type so this is what I call my method add data so refer to the database and use the method in here add data and pass it a new entry so here's where we check by using the boolean so if this equals true and you're toast sounds good probably this say successfully entered else something unhappy face so if yeah so if insert data is true because here we said if it's true it's going to it will return something other than negative one and it will print out this so now that Reds gone away so I guess now I'll do button view a set down click listener for this and this one is going to be easy because I'm going to make another activity for the view all I have to do is switch switch the views you create an intent and we killed it we folded I guess I'll call it view contents view list contents class so I still got to create that class and stuffs not start start activity here and then just pass your intent and that will take it to the other screen so this is this is done so now we just have to create this view list contents class go over here create new class view list contents stands this row what does this one want to extend that same thing okay and then import your oncreate method okay set content you think it's that content you guess accountant do our layout you content layout okay brings all the down reason here create the ListView okay and I can remember if I declared the database helper I think I did your part to put this in my other class yep okay so now I need to make here we need to populate an ArrayList from the database and then and then adapt that to a list adapter so start by making an ArrayList of string type call it the list the cursor so just like before where we had the ad data method that we created we're going to create a cursor here that references the database and references this get list contents right here so that's what this is so the cursor is going to get all the contents of the database by doing this and then what we do is populate the ArrayList with that data oops count zero so if there's nothing in the database nope don't do that in that toast again let's go back here coffee then throw that toast in here this so the base was empty on happy days so then if the database isn't empty you're going to actually do something so while data dot move to next so this it will continue looping through the list dot and string1 so here the reason why I put a 1 in here this reference this is the column number that we're referencing so if we go back to the database the column ID the column here for the ID is 0 this column is 1 so what that's what if I put 0 here it would get the ID but we want the items so I put in 1 we need to make a list adapter it's going to be an ArrayList adapter I don't think I need to put the type in there so this I'm just going to use a basic Android list so 6/10 one share and then we're gonna pass the list here and then we need to set the adapter since this update keeps recommending all kinds of weird stuff there we go okay I think I'm done start the thing down so yeah if there's anything confusing about this don't forget to leave a comment but I think I think I went through pretty pretty slowly and like I said before if you worry about not understanding database helper just straight up copy I'm going to post a source code so just straight up copy and paste this and that way at least you can make apps where you're adding some through database saving it and you can throw it into the ListView kindergarten Oh crashed that's not good Oh forgot to add on I got ad view list contents to the manifest so going to manifest creating your activity like that close it that's probably what it was you don't need to make one friend database helper because it's not it's not it doesn't have an on create method okay let's try the again okay so there's the old stuff that I had in there let's add something new oh no potato and do the contents you can see potatoes out there okay so in in my next tutorial I'm probably going to do a listed I'd show you how to make a list adapter so you can have a list like this a list view but you can have other stuff in the list view so there might be like columns like in this row peanut butter they might have like I don't know a little another bit of text here and so and and so on so I'll make a list adapter probably with two or three things that you can throw it showing the rows so I know that was really hard when I was learning so I mean it's really useful thing if you want to make lists with little extra details in there so if you liked the video like the video show your support and subscribe and thanks for watching
Info
Channel: CodingWithMitch
Views: 167,273
Rating: undefined out of 5
Keywords: Android Studio, Android, ListView, Populate a ListView, Android Application, Simple Application, Learning Android, SQLite, SQLite database, Android Studio Beginners, Viewing Database items, sqlite listview, saving data with sqlite and adding it to a listview, listview sqlite, insert
Id: SK98ayjhk1E
Channel Id: undefined
Length: 19min 5sec (1145 seconds)
Published: Fri May 13 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.