Android ArrayList of custom objects – Save to SharedPreferences – Serializable?

Yes, you can save your composite object in shared preferences. Let’s say.. Student mStudentObject = new Student(); SharedPreferences appSharedPrefs = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext()); Editor prefsEditor = appSharedPrefs.edit(); Gson gson = new Gson(); String json = gson.toJson(mStudentObject); prefsEditor.putString(“MyObject”, json); prefsEditor.commit(); ..and now you can retrieve your object as: SharedPreferences appSharedPrefs = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext()); Gson gson = new … Read more

Serializing private member data

You could use DataContractSerializer (but note you can’t use xml attributes – only xml elements): using System; using System.Runtime.Serialization; using System.Xml; [DataContract] class MyObject { public MyObject(Guid id) { this.id = id; } [DataMember(Name=”Id”)] private Guid id; public Guid Id { get {return id;}} } static class Program { static void Main() { var ser … Read more

How to pass ArrayList from one activity to another? [duplicate]

You can pass an ArrayList<E> the same way, if the E type is Serializable. You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval. Example: ArrayList<String> myList = new ArrayList<String>(); intent.putExtra(“mylist”, myList); In the other Activity: ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra(“mylist”);

What is the difference between Serializable and Externalizable in Java?

To add to the other answers, by implementating java.io.Serializable, you get “automatic” serialization capability for objects of your class. No need to implement any other logic, it’ll just work. The Java runtime will use reflection to figure out how to marshal and unmarshal your objects. In earlier version of Java, reflection was very slow, and … Read more

What is a JavaBean exactly?

A JavaBean is just a standard All properties are private (use getters/setters) A public no-argument constructor Implements Serializable. That’s it. It’s just a convention. Lots of libraries depend on it though. With respect to Serializable, from the API documentation: Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do … Read more