In this chapter we implement the code for listing diary entries. We start by defining our view model:
This code should look familiar to you: we define the LiveData variable diaryEntries, which returns all diary entries contained in the database. The value 0L is passed to the getDiaryEntriesWithEverything() method because with diary entries there is no dummy value ’—’ as with our other data classes. And depending on the variable showDeactivated, deactivated diary entries are displayed or not.
And as with our previous view model, the ViewModelFactory DiaryEntriesListViewModelFactory is defined in the same file, which gets our DiaryEntryDao class as a parameter, so that we can access the database in the ViewModel.
Next, we define the layout file for use in our RecyclerView:
The layout file diary_entry_item.xml defines how our diary entry elements from the database are displayed in the RecyclerView. For this purpose, we use a GridLayout within a CardView: we display the entry’s title, the person’s name, the diary entry’s date on the left side and an (optional) diary entry image on the right side.
The data binding variable diaryEntryWithEverything is defined by means of the data-element, which we access a little further down in the code for our DiaryEntryItemAdapter. Next we define the layout for our fragment:
In fragment_diary_entries_list.xml, a RecyclerView is used to display our diary entries. As with other lists, we also define a text view for displaying an empty list info in case there have no diary entries been stored in the database yet. By means of the data-element, the variable viewModel is defined, which we access in the code in our fragment FragmentDiaryEntriesList.kt, which we will deal a bit later.
Now let’s define our adapter, which implements the Filterable interface, since we want to search items in the recycler view:
There’s not much you haven’t seen in previous chapters. When searching for diary entries in the performFiltering() method, we do this within a couple of attributes: the diary entry’s title, the date, the name of the person, all the assigned tags, and also within the diary entry’s actual text. Notice in the bind method that when displaying the diary entry’s image we check if the user has provided a picture - if not, we display a default icon from our drawables-folder (baseline_edit_note_24).
Next, we look at our diary entries list fragment:
This code is similar to the one for displaying items in previous chapters. As one of the last steps we have to extend our navigation file nav_graph.xml with our new fragment and define actions for the navigation transitions from the home fragment and back (either in the graphical editor or directly in the XML code):
And our final step is to update MainActivity.kt so that we can search within the diary entries list:
With all these new files and extensions of existing files, the code should now rebuild and run on a (virtual) device. If you tap on the menu item ’Diary entries’ in the main menu, an info is shown that there are no entries in the database yet (see Figure 18.1). At the bottom right you will spot the FAB for adding a new diary entry which we cover in the next chapter.