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.
Before we proceed, please make sure that the first two MP1 test cases are
passing: test0_SummaryComparison
and test2_SummaryView
.
Completing the next test requires completing the Summary
static method named filter
.
filter
accepts a List<Summary>
and String
, in that order.
filter
both filters and sorts the passed list 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 List<Summary>
that 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:
If the 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 compareTo
.
Let’s examine how to do that together.
Once your filter 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.java
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.
Right now your goal should be to complete the filter method and UI changes and wrap up MP1! If you get stuck, find us for help on the tutoring site or forum.