Android - Save preferences per object - android

Is there any way to save preferences per complex objects?
I have a dynamic list of objects that each contains 3 strings and 2 dynamic lists of pair of booleans. Is there any way to save those objects like preferences?

You have the option to serialize your complex objects to a file. Then deserialize to recreate the object back from the file. This is not much different from saving to SharedPreferences.
You can refer the code here. https://stackoverflow.com/a/5816861/2107118
Note: Do make your complex object implement Serializable interface and provide a serialVersionUID.

Related

Xamarin listview with objects

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.

How to save a two dimensional array of custom object in a bundle?

I need to save a two dimensional array of custom object in a bundle to restore an activity after a screen rotation but I can't find an example.
You can use Bundle.putSerializable() as arrays are Serializable. You will need to make your custom class Serializable too. Here's a tutorial on implementingSerializable.
How about JSON?
Develop a method to convert your custom object into a JSON object, and add helper methods like "toJSON" & "fromJSON".
Then, you can create a JSONArray for an array of JSON Objects.
A 2D array of this object would simply be a JSONArray of JSONArrays.
Here's an example of what it would look like:
[
[
{"name":"john", "age":25},
{"name":"david", "age": 40}
],
[
{"name":"chris", "age":15},
{"name":"howard", "age":55}
]
]
Not a good idea, mate. I would suggest to save it in somewhere else rather than Bundle. You could still use the same hooker methods onSaveInstanceState & onRestoreInstanceState for the purpose. But, Bundle objects are not designed to store complex data structure. The most it should be handling is a regular object. What you are having is not even an array. It is two dimensions array. You would require a lot of parsing states just to store / restore the data.
How about you put that two dimensional arrays inside your own instance
of Application object and access it from your Activity or
something ? If the device rotates, it will only restarts the Activity.
Not the Application. So, that is one possible solution.
Another one is blocking the Activity to restart when the device rotates. The entire purpose of restarting is to use the different resource for different layout form factor. If you are not using different layout file, you probably won't need the activity to restart.
Anyway, putting complex data outside the Activity scope always helps. There are a lot of state changes and restarting and lifecycle things in Activity. Usually, if you put complex data insides Activity, it is not stable and not reliable because it keeps on changing based on Activity states.
Hope that helps.

"global" array with different data types - Android

I'm new in programming for Android so maybe my question will be very easy to solve but still. I'm trying to make an array of different data types :
I have to add there :
int number
String name
int number_2
int time
int total
And my question now is how to implement it in easiest way, and how to get data from it. In case that I have to get a different records for this variables and store it into list .
Also have a question about way how to keep all values which I handle inside of my array.
I have to keep it because in my program I have to go back to other activities go forward to another and again collect data and add it to my array.
What will be the best and easiest solution ?
Thanks in advance for help
You could create the Array as an Array of Objects. All other classes are derived from Object, so you'll be able to store all types of objects in your Array. However, you would have to check the type of an object you get from the Array, before you'd be able to safely interpret as an object of a specific class. Moreover, you would have to use Integer instead of int.
If all (or at least multiple) of your elements you are intending to store in the Array are belonging to one (physical) entity, you could create a custom Class that holds its own properties as class members, and fill your Array with a list of instances of this Class.
Moreover, if you plan to add elements to your Array, you should use a List instead, e.g. an ArrayList.
As for retaining your data, you would have to either store it in a database, or save it to a file. In either way, you will have to save it upon close of the Activity, and load it again once the Activity starts
To pass the data across activities you will need to pass them using objects you can store in an intent. Seems like the best way to handle that is to either create a PREFS file to store the data or to create an object that extends Parcelable like here:
https://stackoverflow.com/questions/18593619/android-parcelable-object-passing-to-another-activity
Parcelables are preferable assuming you need all the data in a single object, you do not want to "putExtra" a bunch of fields and you also want to be sure data can pass from one activity to another. Otherwise, a simple Util class that reads and writes to a PREFS file is the way to go:
android read/write user preferences
A database is always another option, but seems well outside the scope of your question.

Can I have a main Map file in my Activity and share it with the Fragments?

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.

How to serialize ArrayList

How can I serialize an ArrayList and Save it in a file?
The first thing you need to do is to serialize the class, the objects of which populate the ArrayList. If this is a simple class, composed mainly of primitive types and doesn't have a deep structure, this will be easy. You can choose JSON, protocol buffers or even XML for the representation. If you succeed on this, it will be straightforward to serialize the ArrayList.

Categories

Resources