Android 3 linked values - android

What would be the best way to store multiple 3 linked values (String, String, Boolean)? Like in a HashMap for example.
I need to:
save them in SharedPreferences
load them of sp (of course)
change at least the last value dynamically
get all items where the last value is true (for example)

You have three options:
Store it on preferences
Store it in a database
Save a file on disk
If you want to proceed with preferences I will suggest you to convert the 3 value format to a Json format and store it in preferences as json.
{"value1":"value", "value2":"value", "value3":"value"}
or
{"data":"some data",
"link":{
"data":"other linked data",
"link":{...}
}
}
This kind of data is also stored perfectly in a noSQL database. But if you do not want to add a database at all to your project, maybe you can have a look to some noSQL libraries like SimpleNoSQL (it indeed uses a database behind the scenes, but abstracts you very well from it) or Realm (it stores on disk).

Related

How to store a small list, on Android? SharedPreferences VS SQLite VS file

I have an app that generates objects of a class, let's call it X, that is Serializable. I want the user to be able to occasionally save or delete objects of X from his/her list of favorites (a list that can go up to 100 objects).
What is the most appropriate way to persistently store the list of favorites?
In SharedPreferences, storing the whole list as a JSON String
In an SQLite database:-
Storing one item per object, as BLOB's
Storing one item per object, as JSON Strings
In a custom file, storing the whole list as a JSON String
Some other way?
My thoughts are:
Because adding and removing favorites will be occasional, and the list is small, I probably don't need the advantages of a DB when it comes to searching fields in large amounts of data. So I am inclined to maintain a local ArrayList, add and remove items from it, and save it to SharedPreferences (option 1).
It seems odd to save a key-value pair holding an entire list as a JSON String, I'm afraid I might be unaware of some sort of limitation.
Is there a limit to the size of the String I can store in SharedPrefferences?
Is it too problematic that I add or remove objects from my local ArrayList and then save the whole list?
I am agree with your first approach because of It's manage easily and need small storage space.

Storing a data - Benefit and cons of each storage types

I have this kind of data. This can be or don't be an array. Just for easy reference.
ArrayName = Array1, Array2, Array3
Array1 = abc, cde, fgh
Array2 = abc, cde
Array3 = abc, cde, fgh, ijk, lmn
So, what are the best method to store this kind of data.
If I want to
Add or delete Array1 and all things inside
Add or delete item in Array2(eg. adding fgh or remove cde)
Methods I discovered:
SharedPreference Android Shared preferences example
Arrays
SQLite Android SQLite Example
Text file
Please share the pro and cons of why you choose the method.
Please also share if there are better ways to store this kind of data.
Kindly edit this if you found a better link or sample for other to reference.
Here are pros and cons for each solutions:
1) SharedPreference
You save simple key-value pairs here. So it is very hard to save array and complex structures in SharedPreference. So the solution will not work with arrays and arrays of arrays. It will be extremely(but not impossible) difficult to achieve what you want.
2) Arrays
Absolutely not! It is memory storage, so when you close app, or on process death, you will lose all data
3) SqlLite
I would add to this other Databases for android, like Realm.
Good solution. It is structured storage for collection of data. It will be very easy for storing/retrieving data when it is structured as rows. Furthermore you can delete rows easily. You don't have to read whole structure (other arrays) when you need particular row, or particular array (table in this case)
4) TextFile
I don't recommend to store in a text file, but it is possible to do so, you can serialize those arrays to text file, and deserialize. But every time you have to do this, and to read whole structure and parse it even if you want only e.g. Array2. It can be slow when your data becomes bigger.
It's incredibly hard to give advice with such vague requirements, you apparently have data structured as an array of arrays of strings, and you want to store it persistently on Android - and that's basically all we know.
In addition to the solutions mentioned, I would consider using GSON to store this as JSON to disk. While read/write may not have optimal performance, it's very easy to model documents with things like arrays of arrays, and we have no way of knowing your performance requirements vs ease of use.
class MyData {
public List<List<String>> data;
}
If you then have a MyData object, you could simply serialize it to a string, which could be written to a file on disk:
String json = new Gson().toJson(myData);
This would produce something like
{
"data": [
["abc", "cde", "fgh"],
["abc", "cde"],
["abc", "cde", "fgh", "ijk", "lmn"]
]
}
which could easily be written to disk using e.g. standard File and BufferedWriter. You can then read it back and deserialize using:
MyData myData = new Gson().fromJson(json, MyData.class);

What is the alternative for storing set in sharedPreference using only api 8 in android?

using Set in sharedpreference is API lv11;
i have a project need to parse to many types of nested items
and need to save it to sharedpreference using only string,
the xml items is very complicated to save as a normal string to
sharedpreference, if i use normal string it need to create
so many sharedpreference names and values,
My question is, is JSON is the alternative because its string is
like a list so that i can read easily the items in per category.
You may want to consider using internal storage over SharedPreferences.
Here is more information on internal storage.
Yes you can use JSON as a string and save it to the SharedPreference that is because JSON is faster , smaller and less verbose structure than XML.

Android store last 10 search values

I have an app that searches in a database. I want to store last 10 search values (and I want them to remain even if the user closes the app) to use them as an adapter in AutoCompleteTextView.
SharedPreferences doesn't seem to support arrays or arraylists. What's the best approach here?
You can use a table in a SQLiteDatabase to store the search history, and use the standard SQL API to access it.
Or you can use a file in XML, JSON, YAML, CSV, plain text, or whatever you like to persist the history. The advantage is simplicity and (maybe) performance. The disadvantage is that you'll have to serialize and deserialize yourself (a possible variation is to serialized a Java object directly)
SharedPreferences support boolean int, float, long and String.
BUT ArrayList are Serializable, so if you also declare you object Serializable, you can encode them into a ByteBuffer with an ObjectOutputStream, then convert this byteBuffer into a String, and finally store it into a SharedPreferences, or better to a binary file (as bytebuffer).
In your case, where you just have to save String, it is easer run through every element of the array and save them as "arrayName"+index, and finally save the size of the array.

Array list of double in SharedPreference

I want to know whether it is possible to save as an array list of double in SharedPreference. In my application, I want the 'size' to be saved whenever there is new 'size' written from the user. It must be uploaded to an array list of double, without erasing the previous one.
first of all - the answer to your question is simple:
it's not possible.
but the good news are that there are lots of ways saving array of doubles (as you've been suggested in the comments):
save it to a binary file / serialized file in the internal/external storage, to SQLite database, make it the responsibility of the server side (if there is one..)
you can save the size of the array in the shared preferences if it helps you read the data from the file containing the serialized array, but I guess you already know that..
anyway - good luck

Categories

Resources