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.
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.
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.
How to serialize/deserialize object in android with list of other objects inside ? I done this with sqlite to manually save all fields and in another table objects from list but I am curious is there any easier method.
A fairly easy way to serialize/deserialize an object would be to use JSON and a wrapper like Google Gson that will take care of all the hard work for you.
You haven't mentioned the purpose of serializing/deserializing these nested objects, but Gson will give you a nice little (json parseable) text file to do with as you please.
I need to save List of simple objects
class simple{
String name;
byte[] data;
}
in android. How to do this ? Do I need SQLite or there is easier way ?
Just refer serialization in java, and that makes your objects to be serialized.
see an example here,
http://www.jondev.net/articles/Android_Serialization_Example_%28Java%29
I'd use a database for that. It would make easier to get that data from different activities. Here is a great tutorial on dealing with database in Android.
I assume you can take that byte array as an INTEGER one for database type compatibility (or maybe some clever conversion to something else). The tutorial above provides you with info on the data types.
I receive from a web service a list of json objects that may containing different keys. Eg one would have a title, a url and a list of picture urls, another one would have the same more a phone number... I need to keep these objects in memory but I'm not sure about the best way to do it.
1- I could keep the JSONObject as it is.
2- I could create an array of generic Item object with all the possible fields (most are common to all items) and for each instance set only the fields which have the corresponding keys in the json object, and leave the others null.
3- I could also create for each object a map with the keys contained by the item.
Any thoughts about the best method?
Thanks
Jul
I would look at Jackson or GSON for JSON object mapping.