The following is the first few sections of a chapter from Exploring Android, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Editing an Item

Displaying to-do items is nice. However, right now, all of the to-do items are fake. We need to start allowing the user to fill in their own to-do items.

The first task is to set up an edit fragment. Just as we can click on a to-do item in the list to bring up the details, we need to be able to click on something in the details to be able to edit the description, notes, etc. So, just as we created a DisplayFragment in the preceding tutorial, here we will create an EditFragment and arrange to display it.

This tutorial has many similarities to the preceding one:

The differences come in the layout itself, as we have a different mix of widgets than before. Plus, we need to add an icon to the DisplayFragment, to allow the user to request to edit that to-do item.

You might wonder “hey, shouldn’t we use inheritance or something here?” In theory, we could. In practice, the DisplayFragment is going to change quite a bit in a later tutorial, and so we would have to undo the inheritance work at that point anyway.

This is a continuation of the work we did in the previous tutorial. The book’s GitHub repository contains the results of the previous tutorial as well as the results of completing the work in this tutorial.

Step #1: Creating the Fragment

For the third and last time, we need to set up a fragment.

Right-click over the com.commonsware.todo package in the java/ directory and choose “New” > “Java Class” from the context menu. This will bring up a dialog where we can define a new Java class. For the name, fill in EditFragment. For the superclass, fill in android.support.v4.app.Fragment. Click OK to create the class. That will give you an EditFragment that looks like:

package com.commonsware.todo;

import android.support.v4.app.Fragment;

public class EditFragment extends Fragment {
}

Step #2: Instantiating the Fragment

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

Step #3: Setting Up a Menu Resource

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

Step #4: Showing the Action Item

The preview of this section was whisked away by a shark-infested tornado.

Step #5: Displaying the (Empty) Fragment

The preview of this section is presently indisposed.

Step #6: Creating an Empty Layout

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

Step #7: Setting Up Data Binding

The preview of this section was fed to a gremlin, after midnight.

Step #8: Adding the CheckBox

The preview of this section is unavailable right now, but if you leave your name and number at the sound of the tone, it might get back to you (BEEEEEEEEEEEEP!).

Step #9: Creating the Description Field

The preview of this section may contain nuts.

Step #10: Adding the Notes Field

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

Step #11: Populating the Layout

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

What We Changed

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