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 toolbar button to the app bar, 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 GitLab 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

Yet again, we need to set up a fragment.

Right-click over the com.commonsware.todo package in the java/ directory and choose “New” > “Kotlin File/Class” from the context menu. This will bring up a dialog where we can define a new Kotlin class. For the name, fill in EditFragment. For the kind, choose “Class”. Press Enter or Return to create the class.

That will give you an EditFragment that looks like:

package com.commonsware.todo

class EditFragment {
}

Then, have it extend the Fragment class:

package com.commonsware.todo

import androidx.fragment.app.Fragment

class EditFragment : Fragment() {
}

Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.