How to Save an ArrayList of Custom Objects to SharedPreferences with Gson - Android Studio Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this tutorial we will learn how to save an ArrayList of custom objects into shared preferences using Google's G's on library for this I have prepared a simple recycle of you if you want to know how to build a recycler view you can find the playlist on that in the info box in the top right corner of this video now when I type in some text and those edits fields here and click insert yet a new item to this recycle of you I can add some more just gonna type in some random letters now the problem is when we close the app and reopen it all our objects are gone because we didn't save them and Android doesn't really provide a default way to save an every list of custom objects into shared preferences but we are going to learn how to do that now see on our main activity I have an ArrayList of example items example items as a class I prepared and what we do first is we go to orbital cradle file the one here with module in its name here we go all the way down and Eddie dependency for the GS on library I report a link to this dependency into the description box so just copy it from there next we click sync now now let's prepare our main activity layout so what we are going to do is we gonna add a second button which then saves our list so we create a button wrap content width and height text will be N Save and ID will be a button underscore Save and reporters button next to our and third button okay back to our main activity Java fire in our one create method we are designed as buttons or white button buttons F equals find you by idea adult ID dot button save then we take this button save and calls it on click listener new on click listener and here an hour on click method we wanna save our data also we call a method save data and recreate this method below our on create method to write private wild save data and in here we were save our errorless to share Frances which we do the following way we create an instance of shared preferences with shared preferences shared preferences equals get shared preferences then we have to pass a name in form of a string which will simply be a shared preferences as well and the second argument is the mode which will be a mode underscore private if you want to know more about this you can watch my videos on shared preferences semicolon next we'd our shared preferences edit or so we write edit or we call it edit or equals shared preferences thought edit and we need an instance of Jesus on with Jesus on Jesus on equals new JSON next we create a string J's on which we then contain our ArrayList in JSON form equals json dot to json and then repairs our ArrayList in this case the name of our ArrayList is an example list now we take our edit or dot put string and add this json string to our shared preferences so we give it a key task list and next we pass our JSON variable which we just created in your app you should create constants for those strings it last you recall editor thought apply now when we call this method here it will save our an example list to shared preferences and we also want to loaded ladles we create a method below private words load data and this method will be pretty similar to our save data method we need an instance of shared preferences as well so I'm going to copy this we need G's on to copy this as well we don't need an editor but we also need the string J's on here equals shared preferences thought getstring then we pass our task list tech and the default value to default value will be null in this case so if we didn't save in every list yet we won't get anything back semicolon next rewrite type type now we click on this and press I'd enter and at this one here Java dot length not reflect equals new typed home in this angle brackets rewrite our list then we make another pair of angle brackets and then he repairs our example item which is the class name of the objects our ArrayList contains so you have to add your collosseum then rewrite a space curly brackets and after those curly brackets rewrite dot get type semicolon and then we write an example list equals reason dot from json very similar to our to json in save data day repairs json and our type semicolon and now we load our example list with this method here out of shared preferences but since it can also be null if we didn't save anything yet we have to check here if m example is equals now then we want to create a new one of course so we write m example list equals new ArrayList simple so we eat a lot an ArrayList or we create a new one from scratch ok and now we want to load this data as soon as we start our APIs we go to our on create method in here I originally created a new ArrayList so I'm going to delete this and write instead note data so our app starts we call our load data method then we build our recycler view with this data then we can as usual change our data and when we click on Save button we will save this and the next time we open our app we will load this data back in again ok let's test it because I'm going to type in some random letters here click insert now first let's try without clicking save so I close this app and reopen it and nothing let's create not a few items and this time I will click save close it again we open it and as you can see our data is there if this was helpful please leave a like and if you want more Android tutorials don't forget to subscribe take care
Info
Channel: Coding in Flow
Views: 86,622
Rating: undefined out of 5
Keywords: 2017, android, android apps, android apps programming, android development, android studio, android studio tutorial, android tutorial, apps, beginners, coding, how to make android apps, how to make apps, java, tutorial, android save arraylist, android save arraylist to sharedpreferences, android sharedpreferences, android sharedpreferences custom objects, android sharedpreferences arraylist, android gson, android gson tutorial, android gson fromjson arraylist, gson android studio
Id: jcliHGR3CHo
Channel Id: undefined
Length: 7min 4sec (424 seconds)
Published: Mon Nov 06 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.