• Store and open individual values
  • File handling
  • Store and open objects

SharedPreferences for values

  • Store a single primitive type or a String persistantly
    • Named collections of values
    • Named values
  • Write a value
    String name = "Mike";
    SharedPreferences.Editor storage = 
      getSharedPreferences("mydata", MODE_PRIVATE).edit();
    storage.putString("name", name);
  • Read a value
    SharedPreferences storage = getSharedPreferences("mydata");
    String name = storage.getString("name", "");

Static arrays in Java

  • Arrays are indexed collections of values of the same type
  • Static arrays can't add or remove values
  • Declare and initialize a static Array
    String[] names = new String[] { "Adam", "Betty", "Carl", "Donna" };
    int nameCount = names.length; // The number of items in the array
    names[2] = "Calvin"; // Replace Carl with Calvin
    edit.setText(names[0]); // Show Adam in some EditText view
  • Iterate through all values in an Array
    String welcome = "Welcome to ";
    for (int i = 0; i < names.length; i++) {
      if (i > 0) {
        welcome += ", ";
      welcome += names[i];

Dynamic arrays in Java

  • Can add and remove values during the array's lifetime
  • Declare and add values
    ArrayList<String> names = new ArrayList<String>();
    names.addAll(Arrays.asList("Carl", "Donna"));
  • Iterate through all values in an ArrayList
    String welcome = "Welcome to ";
    for (int i = 0; i < names.size(); i++) {
      if (i > 0) {
        welcome += ", ";
      welcome += names.get(i);

Modify dynamic array

  • Find and remove a value
    int found = names.indexOf("Carl");
    if (found>=0) {

Classes in Java

  • Create your own class with data members
    • Class must be defined in file with same name
    public class Project {
      public String name;
      public int hours;
      public Date deadline;
      public Boolean completed;
  • Create object and set values
    Project p = new Project(); = "Conference Planning";
    p.hours = 16;
    p.deadline = new Date(2014, 5, 1);
    p.completed = false;

Android File System

  • Private directory
    • App has private directory for files no other app can see
    • No extra permissions required
  • External storage
    • Common directory where all files are shared between apps
    • Removable SD card on some devices
    • Needs permissions that user accepts when installing
      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  • Storage Access Framework
    • A more granular approach to sharing files from Android 4.4

File operations

  • Private directory for app
    File dir = getFilesDir();
  • External storage directory shared by all apps with permissions
    File dir = new File(Environment.getExternalStorageDirectory();
  • List contents of directory and show each name in log
    File[] files = dir.listFiles();
    for (int i = 0; i < files.length; i++) {
      Log.i("Found a file with name " + files[i].getName());

Object serialization

  • Store an object and all its members in a file
  • Let Java library handle converting object in memory to bytes on disk
  • For a class to be serializable
    • Must implement Serializable interface
    • Must have static member for version
  • Create your own class
    class MyData implements Serializable {
        private static final long serialVersionUID = 1L;
        public int id;
        public String name;
        public String country;

Complex object serialization

  • If object contains further objects
    • They too will be serialized, if serializable
  • ArrayList
    • Use ArrayList to store any number of objects of the same type
    • Serialize the ArrayList and all contained objects are also serialized

Serialize object to file

  • Open a file for output and write created object(s) to stream
    try {
      MyData obj = new My Data(); // Then set members!
      FileOutputStream output = openFileOutput("data.bin", MODE_PRIVATE);
      ObjectOutputStream serializer = new ObjectOutputStream(output);
    catch (Exception err) {

Read from file

  • Open previous file for input and read object from stream
    try {
      FileInputStream input = openFileInput("data.bin");
      ObjectInputStream serializer = new ObjectInputStream(input);
      MyData obj = serializer.readObject();
    catch (Exception err) {

Databases - not covered in course

  • Required for larger amounts of data
    • All data does not have to be loaded into memory
    • Faster ordering, searching, filtering
  • SQLite
    • Database engine on both Android and iOS
    • Learn the basics of the SQL language
  • ORM - Object Relational Mapping
    • Library that wraps low level SQLite into Java classes
    • No offical library from Google
    • Ormdroid and more