Question

How to save Object in Preferences which looks like this:

public class ToDoList {
public String name;
public String date;
public ArrayList<Product> products = new ArrayList<Product>();
public boolean isChecked;
}

and then loads its values?

Was it helpful?

Solution

You could do it with serialization. Serialization of an object is a short unique String format of an object that can be serialized. Particularly almost every object can be serialized in java except from the View object. You won't have any problem in your case.

How to do it: You should make class ToDoList implement Serializable and all classes that are used inside your object, ex Product. String, boolean ArrayList are serializable so you don't have to do anything. When implementing serialization in an object you have to supply a serial version UID which would be then used to serialize.

So ToDoList would be something like:

public class ToDoList implelements Serializable {
    private static final long serialVersionUID = //generate random by eclipse
    .....
    public ArrayList<Product> products = new ArrayList<Product>();
}

and Product:

public class Product implelements Serializable {
    private static final long serialVersionUID = //generate random by eclipse
    .....
}

then include this static helper class:

public class ObjectSerializeDeserialize {

public static String ObjectSerialization(Object obj)
{
    ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    try
    {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArray);
        objectOutputStream.writeObject(obj);
        objectOutputStream.close();
    }
    catch (Exception e) {
        e.printStackTrace();
        return "";
    }
    return new String(Base64.encode(byteArray.toByteArray(), 0));
}

public static Object ObjectDeserialization(String str)
{
    byte[] byteArray = Base64.decode(str,0);
    Object o;
    try
    {
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(byteArray));
        o = ois.readObject();
        ois.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        return null;
    }
    return o;
}

}

and simply use the following code:

 String todolistSer = ObjectSerializeDeserialize.ObjectSerialization(todolistObj);

the above line of code will return an empty String if something goes wrong and will print the detailed message in the log cat.

Then simply save the todolistSer as a String in preferences and reclaim your object like this:

 ToDoList todolistObj = (ToDoList) ObjectSerializeDeserialize.ObjectDeserialization(todolistString);

suppress any warnings that are issued by the above method and you are done!

P.S. you can use the above solution whenever you have complicated structures that can not be saved as raw variables and you still don't want to use a database

OTHER TIPS

Preferences are simple key ,value pairs. In your case better use SQLite.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top