Google Summer of Code – week # 8

Since I had implemented the Tabbed layout for Abstracts Activity last week with 2 Tabs, one for the main Abstract Content and other for Notes – it was time now to implement ‘Notes‘ feature that’ll give user an option for taking notes against an Abstract if he would want to note something or write anything for that particular Abstract.

Notes:

The first step for implementation of ‘Notes’ was to create a separate table in Database for it.  So i added a Table named ABSTRACT_NOTES in existing database with 4 columns i.e. NOTE_ID, ABSTRACT_UUID, NOTE_TITLE, NOTE_TEXT.  You might be wondering about the ABSTRACT_UUID column here; well that’s intended for maintaining the Abstract UUID of the Abstract for which user is taking the note. The rest of columns are self-explanatory. The query for this table goes like this:

CREATE TABLE IF NOT EXISTS " + TABLE_ABSTRACT_NOTES + "(NOTE_ID INTEGER PRIMARY KEY,
ABSTRACT_UUID VARCHAR NOT NULL, NOTE_TITLE TEXT, NOTE_TEXT TEXT);

-- Query for table creation

Besides this, I also created 3 functions in DatabaseHelper for db functionality of Notes – one for adding notes, second for updating note and the last one for deleting note functionality.  Following is the code for the 3 functions:


//Function for adding notes for some abstract into Database TABLE_ABSTRACT_NOTES
public static void addInABSTRACT_NOTES(String abstractUUID, String noteTitle, String NoteText) {

 ContentValues values = new ContentValues();

 values.put("ABSTRACT_UUID", abstractUUID);

 values.put("NOTE_TITLE", noteTitle);

 values.put("NOTE_TEXT", NoteText);

 long note_id;
 note_id = database.insert(TABLE_ABSTRACT_NOTES, null, values);
 Log.i("GCA-DB", "Note inserted: id = > " + note_id);
}

//Function for delete notes for some abstract from Database TABLE_ABSTRACT_NOTES
public static void deleteFromABSTRACT_NOTES(long id) {
 long rows_affected = database.delete(TABLE_ABSTRACT_NOTES, "NOTE_ID = ?", new String[] { String.valueOf(id)});
 Log.i("GCA-DB", "deleted Note from db - no: " + rows_affected);
}

//Function for updating the Note if user edits it
public static void updateNoteABSTRACT_NOTES(String note_id, String noteTitle, String NoteText) {

 ContentValues values = new ContentValues();

 values.put("NOTE_TITLE", noteTitle);

 values.put("NOTE_TEXT", NoteText);

 long rows_affected = database.update(TABLE_ABSTRACT_NOTES, values, "NOTE_ID = ?", new String[] { note_id} );
 Log.i("GCA-DB", "Updated Note from db - no: " + rows_affected);
}

Next step was the layouts implementation. For displaying the Notes in Notes Fragment, ListView was the best option which I moved for. Since it involved fetching from database, i needed some sort of Cusror implementation. Thanks to the availability of SimpleCursorAdapter in Android SDK, it saved time and hassle as I didn’t have to implement custom cursorAdapter for this. If you don’t know of SimpleCursorAdapter, do check it out – it’s Great thing.

SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
      R.layout.noteslist_item_layout, notesCursor,</pre>
      new String[] {"NOTE_TITLE"}, new int[] { R.id.noteTitle}, 0);

//do Check out the SimpleCursorAdapter documentation for understanding the parameters in constructor.

// NotesCursor is the DB Cursor that has fetched data from db

// NOTE_TITLE is the name of column that's mapped onto R.id.noteTitle TextView

Screenshots – ListView of Notes
notes_module_screenshot_1

After that, I implemented the ListView onItemClickListener so if user clicks on some note, it shows up. Initially I was going for a separate activity for displaying note but then I thought that would be little messy than if note shows up in a pop-up dialog. Thus I tried showing it in DialogFragment. It worked out better and looked Great. (See in the screenshot later)

Like displaying the note in DialogFragment, I decided to implement ‘Adding new Note’ with DialogFragment too. But this was little tricky than the basic dialog. I had to show 2 fields in add new note Dialog where user can input title and text of note that he’s adding. For this purpose, I had to create a separate Layout for the ‘AddNoteDialogFragment and inflate it in onCreateDialog.  The layout file has 2 simple textViews for labels and 2 EditTexts for taking input. (See the final UI in screenshot later)

I reused the same layout file (that was used for AddNoteDialogFragment) for the EditNoteDialogFragment too. The major difference was that in this dialog the title and note text fields are already populated with the existing data and user can edit in those fields and then on clicking of dialog positive button ‘Update’, the note update function is called and the note is updated with the new values for Title and Text.

To provide user the options for Editing and Deleting a note, I had to add Context menu for the ListView so if user long-presses an listview item, the menu pops up with 2 options – EDIT and DELETE. On Clicking Edit, it opens up EditNoteDialogFragment and or clicking Delete, it gets the ID of that Note (from the Cursor _id column) and calls delete function for it.

notes_module_screenshot_2

You might be wondering here that what’s ‘REFRESH’ button for – Well, that’s to refresh the notes list after user edits or adds a new note so that the notes are fetched again from DB and the ListView updates with latest record.

The complete code for this Notes Feature can be found on Github repo of this project – link:
https://github.com/shumail92/GCA-Android/tree/master/Conference-v2/src/com/g_node/gca/abstracts

if you have any question about the code or related, feel free to ask in comments and do share your thoughts and feedback on it in comments below. I’ll be looking forward to any suggestions or feedback.

GSoC Week # 8 – (07 July 2014 – 13 July 2014)

p.s I also worked a little on Schedule section as well in this week, but will cover that in detail in next week blog post.

Leave a Reply