No SQL (Object Serialization)

The current Android framework allows the following means of storage:

  • Shared Preferences
  • Internal Storage
  • External Storage
  • SQLlite Storage
  • Network Storage

http://developer.android.com/guide/topics/data/data-storage.html

Inorder to store bulk data on the cellphone we need to use either External Storage or use a SQLite Database. SQLite database involves complex tasks like connection establishment, query management and integration. External Storage needs crafty design and manual mapping algorithm.

Since everything in Android(Java) is a class, we would love to directly persist the class, rather than first converting it to an SQL Table and then retrieving values individually. ORM(Object Relational Mapping) is currently not implemented on Android, but there is a way we can replicate it using External Storage and Java Serialization. The following steps highlights how we can achieve this.

STEP 1: Declare Class and Implement Serializable.

Here is an example of a Class ItemAttributes, which has an Integer and String data member.

import java.io.IOException;
import java.io.Serializable;
public class ItemAttributes implements Serializable{
    /**
     * Serialized Structure to add ItemAttributes to file
     */
    private String itemName;
    private int itemCost;
    public ItemAttributes(String name, int cost)
    {
        this.itemName = name;
        this.itemCost = cost;
   }
    //Implement Getters and setters for DataMembers you need to serialize
    public String getItemName() {
        return itemName;
    }
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public int getItemCost() {
        return itemCost;
    }
    public void setItemCost(int itemCost) {
        this.itemCost = itemCost;
    }
    //To specify the type of encoding for data members to write.
    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
        out.writeUTF(itemName);
        out.writeInt(itemCost);
    }
    //To specify the type of encoding for data members to read.
    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
        itemName = in.readUTF();
        itemCost = in.readInt();
    }
}

Explanation:

Implement the Serializable Interface:

public class ItemAttributes implements Serializable

Implement Getters and Setters for the Data Members (Recommended but not Neccesary):

    public String getItemName() {
        return itemName;
    }
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public int getItemCost() {
        return itemCost;
    }
    public void setItemCost(int itemCost) {
        this.itemCost = itemCost;
    }

We have to specify to the compiler how to Read & Write each Data Member (The compiler should be able to recognize the behaviour automatically, But in Android2.2¬† the program crashes if you don’t specify the Read and Write Methods for each member.)

    //To specify the type of encoding for data members to write.
    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
        out.writeUTF(itemName);
        out.writeInt(itemCost);
    }
    //To specify the type of encoding for data members to read.
    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
        itemName = in.readUTF();
        itemCost = in.readInt();
    }

Step 2: Writing Objects into External File.

The following snippets of code help us to write the object of Serialized class ItemAttributes in an external file. We can either Persist a single object or instead persist an Arraylist of objects of the serialized class.

 //Make a new ItemAttributes Object
ItemAttributes mItemObject = new ItemAttributes();
mItemObject.setItemName("Umbrella");
mItemObject.setItemCost(5);

 

 //Make a new Arraylist and add object to the arraylist
/**Note: This Step is optional, In case of single object we can directly
 *      Write it to file.
 */
List itemAttributeList = new ArrayList();
itemAttributeList.add((ItemAttributes)mItemObject);

 

//Open file and write the Object or Arraylist of Objects, to the file.
//mContext -> Context of the Application
FileOutputStream fileOutputStream = mContext.openFileOutput("ItemsFile.txt", Context.MODE_PRIVATE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(itemAttributeList);

STEP 3: Retrieving Back the Persisted object
The following code Retrieves back the object stored in the file.

//Open file and read the saved object back.
//mContext -> Context of the Application
FileInputStream fileInputStream = mContext.openFileInput("ItemsFile.txt");

 

//Open File Stream and cast it into array of ItemAttributes
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
List itemAttributesList = (ArrayList)objectInputStream.readObject();

We can now use the Arraylist normally, its loaded in the memory:)

Some points worth noting about serialization: