Android save Integer HashSet - android

I want to save Integer ArrayList to sharedpreferrence with HashSet, but I can only do this with String ArrayList. I have tried converting each of the integers to strings but then I have to change a lot of code. Maybe there is an easier way?
https://i.stack.imgur.com/ba4XB.png

There is no support for lists of items in SharedPreferences (except for String). The easiest way to do this is to write yourself a convenience method that converts an array of integers into a String and vice versa. Then store the String in SharedPreferences.

Related

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

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.

can i store two or more values with same key using SharedPreferences in android?

can i store two or more values with same key using SharedPreferences in android? If no, please tell me how to store values of username, first name, password etc when many users register in registration app?
Ex:
person A registered with username="john12", first name="john" and DOB="06/06/2000".
person B registered with username="arun89", first name="arun" and DOB="08/11/1989".
Now, I want to store these values in SharedPreferences and retrieve them later. Is it possible using SharedPreferences? If not, Please tell me how to do in other way.
Thank you in advance.
I woud consider creating a JSONObject and add the fields you want to store as a key:value pair.
json.putString(key, value);
You can then store the json object in it's string representation with json.toString() and restore it later with
JSONObject jo = new JSONObject(jsonString);
String value = jo.getString(key);
JSONObject also offeres different data types beside strings.
It really depends on how much data you want to store. Depending on that I would choose SharedPreferences or a SQLite implementation.
You cannot store these values directly (as ones added latter will overwrite previously added) but you can always store Parcelable and put your data into it
For your case it is better use SQLIte database.But if you want to use shared preference it is still possible.You have to use a key with additional index to remember different user like
UserName1:arun
UserName2:john
You have to remember the total number of user.Then can maintain all of them.you can also use other data structure like hashmap to maintain data for the shared preference.
I dont't think it is possible, as you don't know the number of users.
You could try to separate the users with commas, but that's lame.
You should consider using SQLite database.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
Then you have to store a array List of user objects first create a class userInfo then create a array List type of userInfo then store the data in this list and put a serialize-able object in SharedPreferences.
You can also store them on a single key called "registers" as string. Concatenate each register to preference. Put ";" (or any other characther you want) between each register. Then parse the string and use the values.
Key: registers
Value: "username=john12, first name=john, DOB=06/06/2000;username=mike12, first name=mike, DOB=06/07/2012"
Using split method of String will give you a list of registers as String.
registers.split(";");
Splitting again with "," will give you properties of each register.

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.

How can i store an Integer Array in SharedPreferences in Android?

I have an Array called Upvalthat has 16 Integer values that I would like to store in my SharedPreferences without creating individual ones for each, but SharedPrefernces won't allow Array's, what is the simplest way of doing this? The declaration looks something like this:
Integer[] UpVal = new Integer[16];
You can store it as a String by transforming it:
Arrays.toString(upVal)
To get it back and convert a String to an Integer array is trivial.
You can serialize an array to String using TextUtils.join(";", myInts) and the deserialize it back using something like TextUtils. SimpleStringSplitter or implement your own TextUtils.StringSplitter.

Categories

Resources