Android store last 10 search values - android

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.

Related

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);

Is it possible to store a HashMap with the value as an ArrayList in SharedPreferences?

I have a HashMap<Integer, ArrayList<MyTeam>> where the class MyTeam is a POJO. There will be at most 5 items in each ArrayList object. Is it possible to save this kind of HashMap to SharedPreferences? If not, what is another alternative? I need this data to be saved when the app is closed and reloaded when it starts up.
I've looked at this answer but the Key and Value attributes are both String and my case is a bit more complicated than just String data types. Will this method still work? Is there a better way?
Is it possible to save this kind of HashMap to SharedPreferences?
Not directly. You are welcome to convert that into JSON and save it as a string preference. Then, to load it back in, you would read in the string and use a JSON parser to reconstitute your objects.
This class demonstrates the basic technique, though it is saving a List<Uri>, not a HashMap<Integer, ArrayList<MyTeam>>. I specifically used this sample to demonstrate the use of the built-in JSONReader and JSONWriter classes; for a real app, I would consider Gson or a similar JSON parsing library.
If not, what is another alternative?
Save it to a SQLite database, probably through a couple of tables. Or, save it to some sort of file, in a file format of your choosing (e.g., JSON, XML).

Android 3 linked values

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).

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.

Not Understanding ? Why do we need BLOB in "Android Sqlite"

I am not understanding why do we need BLOB in SQlite. I have seen this explanation in some of the documents
BLOB. The value is a blob of data, stored exactly as it was input.
But I did not get what it is ? and why is it necessary ? Please provide me some suggestions.
Regards
Anand
BLOB is an acronym that means
B inary L arge OB ject
So, it's a container for stuff that you want stored "as such" without the need to serialize them to eg a clean string, and that are typically larger than the usual types that are stored in a database. Examples are serialized objects (a serialized object is represented by a stream of bytes), bitmaps, sound clips, etc.
when need store ordinary object in database like ImageView at that time
blob type use for that

Categories

Resources