Kotlin
Java

MP1: Search
Java

Created By: Geoffrey Challen
/ Updated: 2022-10-27

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.

Search Method

Before we proceed, please make sure that the first MP1 test case is passing: test0_LoadPlaceFields. You'll have a hard time making any progress until you complete that test case.

Completing the next challenge requires writing a static search method Places.search. You should accept a List<Place> and a String in that order, and return a new List<Place> containing 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.)

If either the List<Place> or String are null, throw an IllegalArgumentException. If the List<Place> is empty, or if the passed String is empty or only whitespace, return the passed List unmodified.

Otherwise, create a new List<Place> and populate it as follows. Trim the passed search String. You should look for whole-word case-insensitive matches for the trimmed search String in the description field of each Place. If a Place matches, add it to the returned List.

Identify word boundaries in the place description by splitting on the space character ' '. But you should first pre-process the description as follows:

  1. Keep all letters, numbers, and whitespace characters
  2. Replace '.', '!', '?', ',', ':', ';' and '/' with a space ' '
  3. Remove all other characters

You will probably want to explore several useful methods on the Character class, including Character.isAlphabetic and similar as you solve this problem.

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.

Search UI

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.

Android App Layout

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.java to enable the search bar.

Discuss why the app looks the way it does. Start in the MainActivity, but also show the layout file, and discuss the relationship between the two. Point out how the MainActivity obtains and uses handles to UI components.

Handling Search Bar Changes

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!

First, discuss the callback design pattern. Point out that the MainActivity needs a way to be notified when the search bar input changes. Describe the idea of registering a callback: one or several methods that are called when the input changes, allowing the UI to update itself appropriately. Then walk through the process of setting up the UI callbacks, and use logging to demonstrate them working. Describe the difference between the two, and the effect of the return value.

Wrapping Up

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 updateShownPlaces. 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.

Your Goal Today

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.

Right now your goal should be to complete the search method and pass the next test cases! If you get stuck, find us for help on the help site or forum.

MP1 Scores

More Practice

Need more practice? Head over to the practice page.