Store and retrieve arrayList from internal memory? - java

I'm building a home replacement app. I need to store the ArrayList with the apps the user picked to show on the launcher in the internal memory. I mean the array mustn't be deleted when the app is closed.
I'm very close to finishing the app and I don't think I'll work a lot more on Java, I'm not a programmer so I just want the easiest way to do it. How can I store and retrieve an ArrayList in the internal memory?

Use SharedPreferenes to store and retrieve the arrayList..
Check this link..
Save ArrayList to SharedPreferences

You should store such data as Arraylists in the database and when you relaunch the app just fetch the data from database(SQLite) and display it to the user.
Heres a good tutorial on android SQLite.
Take a look at this link, for all the different data storage mechanisms in android. But for your requirement I would suggest using a db.

The easiest way is to use Java serialsation. It writes the content of an object to a (file-) outputstream. Thats a few lines of code.
a bit better (faster, less bytes, readable by other non java systems) is to uses a custom serialisation, using DataOuputStream. That creates a binary file in the format you define.

Related

How I can create history layout in android studio such as recent

I am building an app include edit text and button. The user can put text in the edit text, then click button to transfer him to the browser.
So, I would like to create other layout as recent to store the text that the user entered on the edit text.
How I can do that? I need the logic or code that can help me!
Also, should I have create database to store the data?
Example:
enter image description here
You can store data in multiple ways, and you will need to understand what is right for your use case. You can store data in memory, by simply creating a List with your data type and adding to it every time user will click a button, but then it will not persist between sessions with the app.
If you want the data to persist, then you would need to use permanent storage, and there are a lot of options here:
You could use Shared Preferences
You could use File System and save the data to a file
You could use a database i.e SQLite, and store data there
You could use external server, and get the data through REST API.
Generally, there is a good overview of data storage in Android in the documentation which also have code examples.
Every option comes in multiple ways to accomplish it. There are built-in solutions, and multiple libraries to help you with this task, but first of all, you will need to understand what is the predicted usage of this data. I.E Should user have access to the data from another device? Should the data be available offline? Will data have complex structure? How the app can expand in the future? e.t.c.
Only by knowing this you can design how you will handle it.
If you need logic or code to create view, then you will need ListView, or RecyclerView, Adapter for handling the data, extra xml layout file for single item of your data.

Store multiple values in a file - best format?

I want to store multiple values (String, Int and Date) in a file via Java in Android Studio.
I don't have that much experience in that area, so I tried to google a bit, but I didn't get the solution, which I've been looking for. So, maybe you can recommend me something?
What I've tried so far:
Android offers a SharedPreferences feature, which allows a user to save a primitive value for a key. But I have multiple values for a key, so that won't work for me.
Another option is saving data on an external storage medium as file. As far as good. But I want to keep the filesize at minimum and load the file as fast as possible. That's the place, where I can't get ahead. If I directly save all values as simple text, I would need to parse the .txt file per hand to load the data which will take time for multiple entries.
Is there a possibility to save multiple entries with multiple values for a particular key in an efficient way?
No need to reinvent a bicycle. Most probably the best option for your case is using the databases. Look into Sqlite or Realm.
You don’t divulge enough details about your data structure or volume, so it is difficult to give a specific solution.
Generally speaking, you have these three choices.
Serialize a collection
I have multiple values for a key
You could use a Map with a List or Set as its value. This has been discussed countless times on Stack Overflow.
Then use Serialization to write and read to storage.
Text file
Write a text file.
Use Tab-delimited or CSV format if appropriate. I suggest using the Apache Commons CSV library for that.
Database
If you have much data, or concurrency issues with multiple threads, use a database such as the H2 Database Engine.

How to implement a persistent queue in Android

I would like to implement a persistent queue in Android. Essentially a queue where data is stored until another thread takes it and sends it somewhere. The order in which data is stored or consumed is not important (ex. FIFO, LIFO, LILO, etc). Is there a modern library in Android that supports this?
I found a previous question on SOF but this dates from 2012: How to realize a persistent queue on Android
There are a lot of way to do this. You can persist the collection by saving it into a database like SQLite, serialize it as a file and deserialize it when you need it, or you can save it in SharedPreferences of Android.
Anyway there is a cool library called Hawk
(https://github.com/orhanobut/hawk)
that is a
Secure, simple key-value storage for android
How Hawk works:
If you take a look at the Hawk source code you can see that it uses SharedPreferences to save data to the disk. It serializes the object you want to persist and saves it into SharedPreferences as a String.
Quotes and images are taken from Hawk github page.
You can find other alternatives here (in the "Persistence" section) : https://github.com/codepath/android_guides/wiki/Must-Have-Libraries
You can check out Priority Jobqueue
It has an option to persist the jobs and prioritise them.

How can I save the data my app parses, so that it doesn't need to be parsed when the app is reopened later?

So I am working on my app, and it parses data from the LoL (League of Legends) API, so far so good, but the only problem is that if the user closes the app, and reopens it parses the data again. I want the objects I parsed from the JSON data to be saved, once it initially parses them, instead of redownloading them every time I reopen the app. It slows my app down.
There are a few ways to do that, but they all basically doing the same thing: serialize/de-serialize objects. The difference will be mostly what kind of persistence storage and what serialization mechanism are you using.
As for persistence you can use for example file, data base, etc.
Java provides human-unreadable binary serialization. Instead you can use JSON, XML, etc. formats. I assume Java built-in serialization will be the fastest, but it won't be deserialize if your class changed in the mean time.
Local persistence also would allow to map objects you parsed into database and load them pieces by pieces as needed.
Save your JSON data as a string in Android SharedPreferences. It is the most convenient and fastest method for you.
Using SharedPreferences

Best way to store text data in an android app?

I've made an Android application which contains most used German words and sentences. The application contains a CSV file which stores all the data.
Currently it is working as expected but I want to ask if there is a better way to store such data directly in the app?
I'm also thinking about the ability to update the data via internet like adding new words and sentences.
Thanks!
Miretz
If you want to modify the content (update, remove etc.) I would suggest using SQLite DB which has a pretty nice built-in integration with the Android platform.
There are 2 types SQLDatabaseLite and SharedPreference. Major difference between both is that one is organized and the other not so.
If you need a quick use of a storage facility within your app for example changing text sizes between activity SharedPrefference works best for you.
If you have a complex database system where you need more than one data to be saved for a particular event SQLDatabaseLite is for you example of this is spreadsheet of data for customers; Name, Phone Number, etc.

Categories

Resources