The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Tutorial #14 - Saving Notes

It would be nice if the user could add some personal notes to the chapter that she is reading, whether that serves as commentary, points to be researched, complaints about the author’s hair (or lack thereof), or whatever.

So, in this chapter, we will add a new fragment and new activity to allow the user to add notes per chapter, via a large EditText widget. Those notes will be stored in a SQLite database.

This is a continuation of the work we did in the previous tutorial.

You can find the results of the previous tutorial and the results of this tutorial in the book’s GitHub repository:

Step #1: Adding a DatabaseHelper

The first step for working with SQLite is to add an implementation of SQLiteOpenHelper, which we will do here, named DatabaseHelper.

Right-click over the com.commonsware.empublite package in your java/ directory and choose New > Java Class from the context menu. Fill in DatabaseHelper as the name and android.database.sqlite.SQLiteOpenHelper as the superclass. Then, click OK to create the empty class.

Then, replace the contents of that class with the following:


package com.commonsware.empublite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
  private static final String DATABASE_NAME="empublite.db";
  private static final int SCHEMA_VERSION=1;
  private static DatabaseHelper singleton=null;

  synchronized static DatabaseHelper getInstance(Context ctxt) {
    if (singleton == null) {
      singleton=new DatabaseHelper(ctxt.getApplicationContext());
    }

    return(singleton);
  }

  private DatabaseHelper(Context ctxt) {
    super(ctxt, DATABASE_NAME, null, SCHEMA_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE notes (position INTEGER PRIMARY KEY, prose TEXT);");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion,
                        int newVersion) {
    throw new RuntimeException("This should not be called");
  }
}

Step #2: Examining DatabaseHelper

Our initial version of DatabaseHelper has a few things:

As noted in the chapter on databases, it is important to ensure that all threads are accessing the same SQLiteDatabase object, for thread safety. That usually means you hold onto a single SQLiteOpenHelper object. And, in our case, we might want to get at this database from more than one activity. Hence, we go with the singleton approach, so everyone works with the same DatabaseHelper instance.

Step #3: Creating a NoteFragment

The preview of this section was last seen in the Bermuda Triangle.

Step #4: Examining NoteFragment

The preview of this section took that left turn at Albuquerque.

Step #5: Creating the NoteActivity

The preview of this section was lost in the sofa cushions.

Step #6: Examining NoteActivity

The preview of this section is sleeping in.

Step #7: Add Notes to the Action Bar

The preview of this section was lost in the sofa cushions.

Step #8: Defining a NoteLoadedEvent

The preview of this section was stepped on by Godzilla.

Step #9: Loading a Note from the Database

The preview of this section was traded for a bag of magic beans.

Step #10: Loading the Note Into the Fragment

The preview of this section took that left turn at Albuquerque.

Step #11: Updating the Database

The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.

Step #12: Saving the Note

The preview of this section is in an invisible, microscopic font.

Step #13: Adding a Delete Action Bar Item

The preview of this section is in the process of being translated from its native Klingon.

Step #14: Closing the NoteFragment When Deleted

The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.

In Our Next Episode…

The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.