Let’s wrap up MP1! We’ll complete a search method and use it to get the search bar displayed in the UI to work.
We’re about to write a search method together. But before we do, we need to examine a new and very cool Kotlin feature: extension methods! (We set up a stub extension method during our first MP1 lesson, but it’s time to return to it and talk about what’s actually going on.)
Before we proceed, please make sure that the first two MP1 test cases are
Completing the next test requires completing the
List<Summary> extension method named
filter accepts a
String and both filters and sorts the list it is called on based on the passed
String, as described below.
You should trim the passed
String of leading and trailing whitespace, and all
String comparisons should be case insensitive.
Do not modify the
filter is called on.
Filter the list to include only summaries where the search term appears somewhere in the title of the course, where the title
String is defined by the
toString method: for example, “CS 100: Computer Science Orientation”.
Next, you should sort the remaining summaries twice. First, use the default sort you completed earlier. Next, sort the summaries by the position of the search term, with earlier matches appearing first. Return the sorted list of summaries.
For example, consider the following list of summaries:
filter method was passed the
String ” INTRO ”, here’s what you would do:
At this point we have the following ordered list of summaries:
Finally, sort the list by the position of the search term in the title. With the final result being:
Completing the filter method requires sorting the list of summaries in a way different than the default sort provided by
Let’s examine how to do that together.
Now that our search method is working, let’s use it to fix the search bar in your app’s user interface! Doing this is not difficult, but it will serve as our first introduction to how Android’s UI works. So let’s take it step-by-step and not miss the opportunity to learn some new things along the way.
First, let’s examine the layout of our
MainActivity, and begin discussing the design of our app.
Why does it look the way it does?
How could we change that?
This will also provide us with a starting point for the work we’ll need to do in
MainActivity.kt to enable the search bar.
The next thing we need to do is figure out how we can respond to changes to the search bar. This is our first example of a UI event and a callback design pattern used to handle the event, and so we’ll also explore that idea in detail. Once we’re done, you’ll be in a position to wrap up this checkpoint!
At this point we’ve set up our
MainActivity to respond to changes in the search bar.
The last thing to do is to update the list of summaries shown in the list properly!
Here’s what to do. First, perform the search. Then update the list of displayed summaries accordingly.
As a reminder, on lessons where we focus on the machine project we will not assign a homework problem! However, the lesson will usually focus on helping you complete a particular part of the MP test suite, and so we encourage you to spend time on that in lieu of a homework problem.