How to save arraylist in sharedpreferences in android | arraylist in sharedpreferences android

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome back to tech projects i am back with another video so in this video we are going to see how to save array list in shared preferences so most of the time we have to save data like string integers and shared preferences but sometimes there is a problem and on that time you want to save an entire array list of a custom objects in your shared preferences so how you can do that so in this video i am going to tell that method how you can save the entire list in a shared preferences and then you can fetch it easily and then you can use it for your own purpose whatever you want to use so before starting the video it is better to see a demo what we actually are going to create to more clarifications about this i have to open my phone so i have to open my phone and this is the application i open this so whenever you open this application first time so it is showing zero so what it mean whenever user open application first time on that time the list is null and the sizes of the list is zero so that's why it is showing zero now let me add some data here so if i add my name here and h then you can see now it is showing this data so what did it mean i saved this data successfully in my shared preferences and i am just showing the name here but you can show other data as well and you can use this data or in your own way how do you want to use this i am my main motive to teach you how to save entire list and how to fetch entire list of a custom object from shared preferences now let me add one more data and one more thing so you can see all three data are saved successfully now clear that from recent and open this application again so you can see if i open this application again you can see it is showing this data it mean it is fetching from shared preferences and our application is working fine now let me add one more time fetching from shade preferences save it clear that from decent and you can see fetching from sp this is the list of one of the list item so in this video we are going to read this kind of application so this video is going to very helpful and sometimes you need this kind of application or this kind of functionality in your apple application so it is better to watch this video till then so after watching this video if you feel anybody you can comment down below so don't forget now let's start the video and if you don't like youtube then please like the video and if you don't subscribe the channel till now then please subscribe the channel so now let's start the video so click on file the new the new project click on empty activity click on next you can use any language it is your choice and i'm just giving here english shared pref tutorial so our project is building so let's say the program so project build successful click on i have opened my previous project and inside my build.gradle module app i am going to use this dependency so you have to use this dependency as well if you want to save data in your shared preferences so basically we are going to convert an entire arraylist into a string type and then we have and after fetching we have to convert entire string again in our list so that's why we need to use this dependency g sort so after pasting the dependency we are ready to write the code so first of all it is better to paste the layout code entirely at code here because it will going to save over time so if i open my previous project and activate my xml file and i can directly copy paste the entire xml code you can write my own so what it contained it contained this edit text this is the name of the latex ct name this is the edittext tth this is just a button btc and this is just a textview to show the data this is everything packed inside a linear layout hope you understand now i can copy paste some more things like this one [Music] these four datas and this thing as well not distinct sorry this thing find view ids so these themes are done so you can do it by one or we hope you understanding what are these things i am just assigning the xml id to java ids so now if we want to save the data in an arraylist we want to create and custom object so it is depend on you what type of data or what type of list you want to show save you want you can save an integer type a string type list or a custom object type list i am going to demonstrate with the custom object name java class and i can give any class name so i'm just coming here model class now my data contain two things only public string name and second thing publicate each that's it these two things now i need to create a constructor to set these data in my earliest that it so this is done now i need to create an arraylist called model class type okay so this is done now when i have to save the data when someone click on vdc button vdsa dot set on click list no i need to save the data and what data i need to save i need to save the name and age so i have to face these things from my editors to string adh dot get text to string that's it hope you understand now create this function and this is name change the function parameters name and this is h so we are going to save this now let us suppose user open the application first time so on that time first of all we have to check whether the shared preferences contain something or not and if it is contain something then we have to show that data so i need to also call a function here without data whenever user open application first time or anything so this is just a function load data to check whether the shared preferences contain something or not so now the main work is going to written inside our save data function and load data function so first of all let's see the functionality how to save data and share preferences so first of all declare a variable of sale preferences share preferences shared preferences equal get application context and let me script dot get shared preferences and give any reference name so you can give any new resources i just give it this is just a name where your all data is going to store so i just give here data and mode is private that's it now we need to use a editor here to save data and share preferences serial preferences editor will give anywhere you wanna keep editor shared preferences dot edit that's it now we are going to use json to convert our array list into a string type so json json equal new json first of all we have to declare a variable for this now now after this first of all we have to add the data in our arraylist so what is our arraylist name so sorry i forgot to give the name aurelius so this is our list where we have to add the data out of this and what we want to add we want to add our own data like name and age so how you can we can create the object format of class and i can pass the name here and second thing integer and i can pass the age here so it will going to add this object inside our arraylist and then i need to save this in my shared preferences now create a variable for a string type to store convert an entire array list into a json format or stream format so g sort to json and inside this past your playlist and now we need to store this g sort so editor dot put string passes student data you can give any variable name this is your table name basically you can say which will help to fetch that variable data as well so this is them now you just need to edit it dot apply that's it so this is the and after this you can show the data and on our text view i want to show the data this data and the data which is present so it is yours whether you want to show data after saving or not i am just showing and that's it and after this i need to call the load data function again to fetch all data all new data from our shared preferences so this is the code to save the data in your shared preferences if you want to save array list now let's see how to help the data so write code inside this load data function so first of all we have to perform this same thing we need to create a variable for shared preferences type sharepreferences and give the same name so what is the name what name i am saying this data name should be shame because this is your file name where your data is going to save now after this again give a variable json to convert the string into json format then we are going to get the json type string from our shape instance so say it preferences dot get string and the string name is student data student data and by default if it is not present anything then it will going to return null as well so you can see this is our file name student data hope you understand give the same name otherwise it will not going to work now we need to use type token here so for why we need to use type table because we have to test the data from our shared preferences and then after need we need to convert that string into an rf type so that's why we need to use typography so type type equal so don't import this type right now it is showing error so don't worry and then new type token and it will going to return the list and the type of the release is class and after this just type here get done that's it now import this type token first so alt enter when you click on alt enter it will going to import this so this is the type token of json type so make sure you type this gson type and now click on this alt enter in on type so it will automatically going to import the first one which is your suitable one so this is them now after this we need to store our entire list inside our gson so gsum dot from json and here we need to pass our json string and the type of the string so this is done now we can say user and a check if our list equally columnar then it means no data is present here so on that time i can create a new url so that user can save the data in our early list tv size dot set text and at that time i can show the list size so zero it will be one two zero and if it is present here then all the time i need to show the data all entire data so i am going to use a for loop here in i equal 0 i less than arraylist.size i plus plus and tvsize.txt and what i want to show i want to show first of all the previous data as well so don't get text to stream then i need to concatenate backspace and to change the new line and then show the name here so arraylist dot get i dot name so you can use this data in your own way it is your choice and then i am again concatenate tax person to change so finally you guys have replication rather and now it's time to check whether that is working or not everybody is working well then we will end this video so application built successfully we're going to install the application inside our phone application stands up as well so you can see one first time when our user open that first time the application data is null so that's why it is showing zero we set the zero here now let me add one thing so this is first data save so it is saved now this is second saved now close this application from reason and open this so which is the application this is one tutorial one so you can see this is first and second data is present here now let's make a final check of the application so it is working decent so this is working so i hope guys you learn something new in this video if you have any query if you have any questions just comment down below i will try to give the answer of every comment please like the video please subscribe channel please share the channel if you have any questions just comment down below so thank you guys see you soon in next amazing video thank you
Info
Channel: Tech Projects
Views: 11,874
Rating: undefined out of 5
Keywords: shared preferences in android studio, save array list in shared preferences, how to save array list in shared preferences, how to save arraylist in sharedpreferences in android, How to Save ArrayList in Shared Preferences, SharedPreferences store Array or ArrayList Android, Save and Load entire List in Shared Preferences in Android Studio, Android Permanent ArrayList Data Storage Using Shared Preference
Id: xjOyvwRinK8
Channel Id: undefined
Length: 12min 37sec (757 seconds)
Published: Sun Apr 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.