In this chapter we want to realise the display of persons, which is shown when the user taps the menu item ”Persons” in the main menu. The display is done in a separate fragment PersonsFragment.kt, whose user interface (UI) is defined in the layout file fragment_persons_list.xml. Similar to fragment_genders_list.xml, this XML file essentially consists of a RecyclerView element that fills its contents with the help of the additional layout file person_item.xml and the adapter PersonItemAdapter.kt. The file is slightly more complex since we add a couple of attributes to the name, especially a picture of the person. As with genders, parallel to the fragment PersonsFragment.kt we also create a ViewModel PersonsListViewModel.kt. As already familiar from the previous chapters, we again have to extend existing code files. Let’s start with the view model:
This code should look familiar to you: we define the LiveData variable persons, which returns all persons contained in the database. The value 1L is passed to the getAllPersons() method so that the dummy value ”—” is not displayed in our RecyclerView. And depending on the variable showDeactivated, deactivated persons are displayed or not.
And as with GendersListViewModel, the ViewModelFactory PersonsListViewModelFactory is defined in the same file, which gets our PersonDao class as a parameter, so that we can access the database in the ViewModel.
Next, we extend our light mode theme, defining an id called no_entries_text_id and a style called CrudDiary_NoEntries which are used to display a note at the start of the persons list in case no persons have been stored in the database yet. We are also adding a few more styles for displaying our persons list:
As usual, we need a couple of additional strings in our string.xml files:
Next, we define the layout file person_item.xml:
The layout file person_item.xml defines how our person elements from the database are displayed in the RecyclerView. For this purpose, we use a CardView that is visually equipped by means of styles previously defined in themes.xml. In comparison to gener_item.xml, we show more information about each person: the name, an (optional) image, and the person’s birthdate. The data binding variable person is defined by means of the data-element, which we access a little further down in the code for our PersonItemAdapter.
In fragment_persons_list.xml, a RecyclerView is used to display our persons. As mentioned above, we also define a text view for displaying an empty list info in case there have no persons 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 FragmentPersonsList.kt, which we will deal a bit later. Next we tackle the adapter for our persons list. Since we will use the Picasso framework for handling images in our recycler views, let’s first extend our build.xml file:
Now we’re ready to define our adapter, which implements the Filterable interface, since we want to demonstrate the capability of searching in a recycler view:
We have to override a couple of methods for fullfilling the Filterabel interface. Mostly, this code has been adapted from a posting on stackoverflow.com. The method performFiltering() in the customFilter variable is defined in a way that only persons are returned whose name or birthdate match the passed constraint char sequence. The bind method is similar to the adapter for genders, however we additionally check, if the person is identical to the main user (who can be set in the settings menu which we will cover in a later chapter). If the person equals the main user, we highlight this person by using an increased border stroke width and a different border color. And we check, if an image is available for the person in which case we use the Picasso framework for displaying the given picture.
Next, we need a new variable someFilterableAdapter in our GlobalAppStore:
In this variable we store the current adapter object so (which implements the Filterable interface) that we can refer to it in our search box. Next, we look at our persons list fragment:
This code is similar to the one for displaying genders. However, we check for the size of the person list and depending on its size we toggle the visibility of our empty list info textview. Furthermore, we set the adapter in the GlobalAppStore’s someFilteralbeAdapter variable. As a last step 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):
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 ’Persons’ in the main menu, an info is shown that there are no persons in the database yet (see Figure 12.1). At the bottom right you can see the FAB with which we will be able to add a new person in the next chapter.