Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 27  Search


Next, clear the stored query (set it to null) whenever the user selects the Clear Search item from the
overflow menu.


Listing 27.13  Clearing stored query (PhotoGalleryFragment.java)


public class PhotoGalleryFragment extends Fragment {
...
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
...
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_clear:
QueryPreferences.setStoredQuery(getActivity(), null);
updateItems();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
...
}


Note that you call updateItems() after you update the stored query, just as you did in Listing 27.12.
This ensures that the images displayed in the RecyclerView reflect the most recent search query.

Free download pdf