I have following ArrayList in Android:
private ArrayList<Integer> Array= new ArrayList<Integer>();
It will grow over time via add()
It will approximately contain up to 50 elements.
I want to store it persistent. I was thinking of xml, sharedpreferences and DB, but I am not sure what is the best way to go.
Saving to SharedPrefs is probably the quickest.
Here is a question that shows how to marshal an array of strings into json and then store in SharedPreferences (and read them back again).
Just change the String array to an Integer array and you're done.
How can write code to make sharedpreferences for array in android?
I would use whatever else you are using in your app - ie keep similar things in similar places.
One benefit of using sharedpreferences is that they can be backed up (if you implement that service)... However using a DB may give you more flexibility in the future.
That doesn't really answer the question though. From how you describe it, it does smell like preferences would be better though.
Related
If I have a class containing of integers, strings and a Date, how should I go about creating, saving and displaying a new instance of this class in my ListViews?
I've used SharedPreferences before for saving local strings but is this possible with objects as well, or should I look into SQLlite?
Edit: Also, if SQLlite is needed, a little steer in the right direction would be appreciated.
You can still use preferences to save your objects but you need to serialize them into strings. And for later use you have to deserialize them from strings into your list of objects. This can easily be done with Json.net but I would recommend to use a database like SQLite. This would make it easy if new requirements like searching or object extension comes up.
A good entry into SQLite and Xamarin can be found here or here.
I've found several similar pages that haven't quite been able to address my dilemna. Here goes:
I would like to be able to create a SharedPreferences list in Android that is extractable with an iterator (loop). Somehow I need to be able to add to a list with a key-value of "[Arrayname]+[element_number]" format. I also need to be able to add/remove values at will.
-- How will I be able to add to the end if I don't know how long the current SharedPrefs list is?
-- I also need the length of the SharedPrefs list to create arrays from it.
You get the a map of all values contained into a SharedPreferences instance with the getAll() method. You can then convert it to a list.
I suggest working with a regular in data container (e.g. ArrayList or HashMap).
you can store the container in the shared preference by serializing it:
https://stackoverflow.com/a/5816861/1393632
Now you just need to keep it in sync with your shared preference when updating it.
I want to make an Android application which shows questions on the go on the basis of user selection. But I won't use a server, and so the questions have to be bundled with the app. But adding the whole questions would not be a great design, so either SQLite database can be used, or xml metadata can be used. But SQlite bundling I heard makes the app large in size. Is that so? And could someone explain how to refer to a xml file with self-defined metadata, to create questions on the fly. What will be the best way to do this?
the SQLite DB is on the phone already, I've used it a few times with no big jump in .apk size.
If you are looking for an easy-to-use stored hashmap, try SharedPreferences! Though I wouldn't use it for a heavy solution, the implementation guarantees ACID and is very straightforward. you can make several different hashmaps and name them different things with SharedPreferences http://developer.android.com/reference/android/content/SharedPreferences.html
I declare:
private SharedPreferences anchorHash;
in onCreate:
anchorHash = getSharedPreferences(getString(R.string.anchor_hash), MODE_PRIVATE);
inonPause:
SharedPreferences.Editor editor = anchorHash.edit();
editor.clear();
for( String s: anchors.keySet()){
//it's a another hashMap I want to write here, but you can do this however you like
editor.putString(s, anchors.get(s));
}
editor.apply();
//or commit() if you need to know about the success ( it'll happen )
I am currently using SharedPreferences for both my preferences and my data. Realizing that getting all of the values for my data out via prefs.getAll() actually gets both of my SharedPreferences which is incredibly annoying.
What is the best route for my data. It is key-value with the keys being dates and the values being floats. (Actually ideally I'd have two floats for every key, but I could traverse two.)
Can I make a Hash Map in my activity and inflate/deflate as normally and send it to my fragments as I need the data?
Your question is asking a few different things, so I'll try answer them all.
For storing data that is too complex for shared preferences, you should look into using an SQLite database. There are some good libraries that make it very simple - check out ActiveAndroid or OrmLite.
If you want to stick with shared preferences, but want to solve the issue of getAll returning the preferences and the data, you can actually create 2 separate sharedpreferences. There is a method getSharedPreferences (String name, int mode) which takes a name. Use the default shared preferences for your preferences, and create a shared preference with a different name for your data.
As for sending data to your fragments, you can use a Bundle. Bundles take all sorts of data, and serializables as well, so that should be no problem. Put your data into a bundle and pass it to your fragment when its instantiated.
I have checkboxes in my custom listview, I'm using a boolean array to save the state of these checkboxes. I want to make the state of checkboxes persistent through out the lifetime of the app.I know that this can be achieved through sharedpreferences, but I don't exactly know how this can be done.
I know that this can be achieved through sharedpreferences, but I don't exactly know how this can be done.
There is no option to push serializable objects into sharedpreferences. Because of that, you'll be forced to convert the boolean array to one of the supported types. The only one I can see making sense would be to convert the state of the array into a string like :
"0|1|0|1|1"
Then push that into the shared preferences. To do this you could use the Arrays.toString(boolean []). You will, however, have to write a parse method for extracting the value back out from the SharedPreferences. That is probably the easiest option to accomplish this.