Let's make more progress on 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 MP1 test case is passing:
You'll have a hard time making any progress until you complete that test case.
Completing the next challenge requires writing a
List<Place> extension method named
You should accept a
String in that order and return a new
places that match the search
String according to the specification provided below.
(This is probably the part of the MP that is most like a homework problem—at least so far.)
List<Place> is empty, or if the passed
String is empty or only whitespace, return the passed
Otherwise, create a new
List<Place> and populate it as follows.
Trim the passed search
You should look for whole-word case-insensitive matches for the trimmed search
String in the
description property of each
Place matches, add it to the returned
Identify word boundaries in the place description by splitting on the space character ' '. But you should first pre-process the description as follows:
You will probably want to explore several useful methods on the
Character class, including
Character.isAlphabetic and similar as you solve this problem.
Character.isAlphabetic accepts a
Int code, not a
Char, but you can retrieve the code as
As an example, consider the following description from your favorite places dataset:
The following search
Strings would match this description: "trex", "CHEESEBURGER", " either ", "THEYRE".
The following search
Strings would not match this description: "either.", "T-rex", "with it".
You can probably think up better ways to approach this search problem. But this will do for now.
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 callback design pattern, 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 places shown on the map properly!
Here's what to do. First, perform the search. If the search yields no results, show all places on the map. Otherwise, show only the places that match that search.
You'll want to review how to use
Don't duplicate that logic!
The correct solution here is only a few lines of code.
If you find yourself writing a lot more, reach out for help.
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.
Need more practice? Head over to the practice page.