Saving and loading ArrayLists in Android - android

I am working on an android application that will randomly choose a activity for people to do from an array list. I want this list to be edited by the user of the application by adding or removing activities from the list. I would like the list to be loaded when the application is opened and saved when paused, closed, or destroyed.
I cannot figure out how to store the array list an load it. I have tried to understand posts in the past about this topic but cannot make sense of how to implement them.
I you can help, it would be greatly appreciated.

You have some alternatives:
1) Use a local database (I suggest greendao for the great performance). Here's the oficial page
That way like in a conventional database you store (in the OnPause method) the data and query it the next time the user opens the app.
2) Use serialization (its convenient for small amount of data), as the previous one in the onPause method you serialize the object and deserialize it when the users open the app. Here's a tutorial about it

Related

Temporary and static realm objects used together?

I am currently running into some issues regarding the realm database and I don't know what the best practices are to tackle this problem.
So I have setup an app which communicates to a server and gets Post Objects through Retrofit stored into my Realm. This post feed is a core part of my app and I want to keep things locally to stay attractive while being offline. The thing is that I cannot store the entire feed list locally as this would be a massive chunk of memory. But I want to make sure that the User gets this content while scrolling through a recycler-view.
The recycler-view currently only shows local Posts and updates them if refresh is forced. I want to implement a load on scroll mecanism that loads Post objects into it while scrolling but this scroll objects should not be stored locally when the app closes.
I thought of creating an additionally in-memory Realm but there is another problem: A Post Object contains a ForeignKey to a UserObject. When I download this dynamic PostObjects and a UserObject changed over this time on the server I want to be able to reflect this changes to every other User on the local persisten Realm too. (To avoid having 2 different UserObjects for the same User)
My best idea i came up so far is to have a
Boolean field on the PostObjects set to true or false to indicate temporary state or not. After the applications closes I would drop all the temporary entries. Is this a viable solution or do I miss anything? I hope you understand my problem and can help.
Edit: My Realm Database contains two relevant objects:
User
Has a name (primarykey), id (global from server), imageUrls etc..
Post
Has a User who created it, again an id and its data (text, date, images...)
Edit2 : What I really need is a type of in-memory Realm that allows me on start up to clone another realm that is used to have data in case of no connectivity. Before the in-memory realm gets deleted I would then override the offline realm with the 5-10 last post entries of the temporary realm. Is such a thing possible?

Android - Already Loaded data not display when first time start the application from recent app

In my application, I have some data to display in the dashboard which are coming from the sqlite db. But when I put the app to the recent app and open the app from the recent app list on next day, those data don't display and showing the error message (that I set to show when there is no data in the sqlite).
To give a better idea about my issue, those are the basic steps.
Install application
Open Application
Navigate through pages
Keep the device
Open Application on Next day
Inspect the behaviour of Dashboard(No data showing)
I think android may auto kill the apps in the recent app.isn't it?.
How can I solve this issue?.
If the data that you are storing are only with respect to your Activity and other classes, Android will clear off data as per priority for availing memory.
This is applicable to static variables also. Resolution for this will be storing the data using Parcelable interface.
Store data either using Parcelable or in Sqlite.
Retrieve the same when the Activity/Fragment is loaded again
Use plugin's available to generate these objects for ease of programming like android-parcelable-intellij-plugin-kotlin
For sample implementation use this reference StackOverflowLink
I don't know how the app flow have been designed since there is no source code, but the following lifecycle will help you figure out the point where you are losing your data (I am assuming you are talking about the unsaved data).
Based on this, you might have to implement the onPause() and onResume() accordingly.

One-time download ListView data - Service or Bundle?

I'm developing an app that downloads ParseObject's data from server and populate a ListView in Fragment with it.
I read about downloading data by Service and after it's done (some kind of listener?) it would update Fragment and be accessible until user leaves the app (which is fine by me).
On the other hand - I can just store it in Bundle and retrieve it every time I get back to that Fragment, but then I'd need implement Serializable which in this case can be cumbersome: like here
Fragments are held by DrawerLayout so it's really irritating now to see loading bar everytime You change to that Fragment and I'm looking for a solution to change that to improve UX.
What do You suggest? Which approach would be better in that situation? Maybe there are things that I should be aware of before attempting to use any of these?
I think it depends on how often the data is changed on the server.
If only daily/weekly the solution must differ compared to if the data is changed every mins/hours.
If the data is kind of static then you can download it only once, and save it to SharedPreferences, or to a seperate local file or DB.
If it changes kinda often, I'd suggest to use bundles or in memory objects, so when the user reenters the app the data should be downloaded again.
The solution i'd use would be to simply convert the entire list of data to JSON, and then save it in SharedPreferences. This allows for easy reuse of the data when the user goes back to that fragment.
As you aren't saving the data across app close/reopen, a local database is not needed.

ArrayList or SQLite

In terms of storing data in Android, would it be more efficient to use a large ArrayList or setup an SQLite database? I have ~9000 bus stops and stop names (both in String) that I need to store and I was wondering which method would be the quickest or most efficient.
An ArrayList is probably a very bad idea. I assume you want the data to be persistent, so if your user kills your app your data will be lost if you use an ArrayList. A database is persistent(unless the user clears the cache of the app). So I would highly recommend using a database over an ArrayList.
If your data does not change then you could probably have it read on startup and kept in memory while the App runs. For example, having a .json file with all the stops, read it on startup, put the data in a HashMap or something that is easy to use since you will probably lookup stuff. It would probably work fine with ~9000 entries.
If you are going to add or update information about the stops during usage then the SQLite is the better choice though.
1.) Store and retrieve your data from a SQLite DB since you want persistance. And since you say you have 9k+ rows a simple select will give you everything at once and you can easily filter the data as well if you need to
2.) At startup, put all your data into efficient memory structures like HashMaps(or in your case arraylists) and reference them throughout the app. This way you'll only do one time access.
3.) When in doubt build a DB. No harm, more efficient and easier to handle than files

Android, suggestions on storing temporary data

Its an application that is calling the ZXing barcode reader on button push, after scanning in shows you the code and you can put in an amount (how much you want to order from that product), then you go next scan, so on so on...I need to store this data. In my main window under the scan button, i will have a show scanned list, with all the scanned items plus the amount of each. And i will have a send button which sends a this list in a .txt or a .csv file via bluetooth to PC. And after closing the application with exit button, or with the back key, this list needs to be destroyed , so when i start the application again, there is a fresh, new list. First i thought, on using SQLite, but my costumer wants a simple program, without db. I could just need a few ideas on storing, data else how. Like an array list, but i dont know how hard is it to handle such a storing bethod, with editing or deleting and such. Or directly saving it into a .CSV file? Any idea is welcome.
Thanks you.
create a Bean class say Product which contains the attributes which you want to display on your list view.
Take a ArrayList
store the bean objects in arraylist and finally iterate it to do your calculations.
To get the CSV, you can call the to string on the instance of arraylist.
IMO using SQLite would be your best option, it does not add that much complexity to your app. Though if you really do not want to use it, Shared Preferences is an even simpler API for storing data.

Categories

Resources