title: How to hide items from recyclerview as I search in a List<>
body: <p>I am using a recyclerView to show a grid of movie posters. The posters are contained in a List&lt;> along with their respective title and so on.</p>
  
  <p>I implemented a searchView widget and I can successuflly get a List of matching results. But I can't hide the other ones.</p>
  
  <p>As you understand I don't want to delete the irrelevant movies from the adapter or the user would not be able to see them again.</p>
  
  <p>This is the code:</p>
  
  <pre><code>public class SearchUtils {
  
      public static List&lt;String&gt; search(List&lt;Show&gt; list, String keyword){
          List&lt;String&gt; results = new ArrayList&lt;&gt;();
  
          for (Show curVal : list){
              String curTitle = curVal.getTitle().toLowerCase().trim();
              if (curTitle.contains(keyword)){
                  results.add(curTitle);
              }else{
                  results = new ArrayList&lt;&gt;();
              }
          }
          return results;
  
      }
  }
  </code></pre>
  
  <p>ListFragment.java</p>
  
  <pre><code>public class ListFragment extends Fragment implements LoaderManager.LoaderCallbacks&lt;List&lt;Show&gt;&gt; {
      private static final String LOG_TAG = "ListFragment";
      private static final String ARG_SCOPE = "com.dcs.shows.activity_to_launch";
      private static final String BASE_URL = "http://api.themoviedb.org/3";
      private TextView tv;
      private ProgressBar pb;
      private int scope;
      private RecyclerView mRecyclerView;
      private ShowAdapter mShowAdapter;
      private SearchView mSearchView;
  
  
      public static ListFragment newInstance(int target) {
          Bundle args = new Bundle();
          args.putInt(ARG_SCOPE, target);
          ListFragment fragment = new ListFragment();
          fragment.setArguments(args);
          return fragment;
      }
  
      public ListFragment() {
      }
  
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          scope = getArguments().getInt(ARG_SCOPE);
          setHasOptionsMenu(true);
          Log.i(LOG_TAG, "onCreate@Scope is: " + scope);
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_list, container, false);
  
          mShowAdapter = new ShowAdapter(new ArrayList&lt;Show&gt;());
          mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
          GridLayoutManager glm = new GridLayoutManager(getActivity(), 4);
          mRecyclerView.setLayoutManager(glm);
          mRecyclerView.addItemDecoration(new SpacesItemDecoration(8, getActivity()));
          mRecyclerView.setAdapter(mShowAdapter);
          mRecyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(glm) {
              @Override
              public void onLoadMore(int page, int totalItemsCount) {
                  // Triggered only when new data needs to be appended to the list
                  // Add whatever code is needed to append new items to the bottom of the list
              }
          });
  
          pb = (ProgressBar)rootView.findViewById(R.id.progress_view);
  
          ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
          if (networkInfo != null &amp;&amp; networkInfo.isConnected()) {
              // fetch data
              getLoaderManager().initLoader(1, null, this);
          } else {
              // display error
              pb.setVisibility(View.GONE);
  
          }
  
          return rootView;
      }
  
  
  
      List&lt;Show&gt; searchList;
  
      @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          super.onCreateOptionsMenu(menu, inflater);
  
  
          getActivity().getMenuInflater().inflate(R.menu.main, menu);
          final MenuItem myActionMenuItem = menu.findItem( R.id.action_search);
          mSearchView = (SearchView) myActionMenuItem.getActionView();
          mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
              @Override
              public boolean onQueryTextSubmit(String s) {
                  if(!mSearchView.isIconified()) {
                      mSearchView.setIconified(true);
                  }
                  myActionMenuItem.collapseActionView();
                  return false;
              }
              @Override
              public boolean onQueryTextChange(String s) {
                  if(s != null || !s.isEmpty()) {
                      for(Show movie : mShowAdapter.getList()) {
                          if(movie.getTitle().toLowerCase().contains(s.toLowerCase())){
                              mShowAdapter.add(movie);
                          }
                          mShowAdapter.notifyDataSetChanged();
                      }
                  } else {
                      mShowAdapter.addItemsToList(searchList, false);
                  }
                  return false;
              }
  
          });
          mSearchView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
  
              @Override
              public void onViewDetachedFromWindow(View arg0) {
                  // search was detached/closed
                  Log.v(LOG_TAG, "Restoring list: " + searchList + " size: " + searchList.size());
                  mShowAdapter.addItemsToList(searchList, false);
              }
  
              @Override
              public void onViewAttachedToWindow(View arg0) {
                  // search was opened
                  searchList = mShowAdapter.getList();
              }
          });
      }
  
  
  
  
  
  
  
      private class ShowHolder extends RecyclerView.ViewHolder {
          public ImageView mImageView;
          public TextView mTextView;
  
          public ShowHolder(View itemView) {
              super(itemView);
              mImageView = (ImageView) itemView.findViewById(R.id.grid_item_image);
              mTextView = (TextView) itemView.findViewById(R.id.grid_item_title);
          }
  
      }
  
      private class ShowAdapter extends RecyclerView.Adapter&lt;ShowHolder&gt; {
          private List&lt;Show&gt; mShows;
  
          public ShowAdapter(List&lt;Show&gt; shows) {
              mShows = shows;
          }
          public void add(Show show){
              mShows.add(show);
              notifyDataSetChanged();
          }
          public void addItemsToList(List&lt;Show&gt; newShows, boolean append){
              if(append){
                  mShows.addAll(newShows);
              }else {
                  mShows = newShows;
              }
              notifyDataSetChanged();
          }
  
          public void removeItemsFromList(int index){
              mShows.remove(index);
              notifyItemRemoved(index);
          }
  
  
          public List&lt;Show&gt; getList(){
              return mShows;
          }
  
          @Override
          public ShowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
              LayoutInflater inflater = LayoutInflater.from(getActivity());
              View rootView = inflater.inflate(R.layout.list_item_row, parent, false);
              return new ShowHolder(rootView);
          }
          @Override
          public void onBindViewHolder(ShowHolder holder, int position) {
              Show currentShow = mShows.get(position);
  
              holder.mTextView.setText(currentShow.getTitle());
              Glide.with(getActivity()).load(currentShow.getImage()).into(holder.mImageView);
  
          }
          @Override
          public int getItemCount() {
              return mShows.size();
          }
      }
  
  
  
      @Override
      public Loader&lt;List&lt;Show&gt;&gt; onCreateLoader(int i, Bundle bundle) {
          //start the loader with the appropriate uri
          //for now it only supports movies+popular
          //it will support movies+top, tv+popular, tv+top.
          Uri baseUri = Uri.parse(BASE_URL);
          Uri.Builder uriBuilder = baseUri.buildUpon();
          uriBuilder.appendPath("movie");
          uriBuilder.appendPath("popular");
          uriBuilder.appendQueryParameter("api_key", QueryUtils.API_KEY);
          uriBuilder.appendQueryParameter("page", Integer.valueOf(1).toString());
  
          Log.v(LOG_TAG, "onCreateLoader@URL built: " + uriBuilder.toString());
  
          return new ShowLoader(getActivity(), uriBuilder.toString());
      }
  
  
      @Override
      public void onLoadFinished(Loader&lt;List&lt;Show&gt;&gt; loader, List&lt;Show&gt; shows) {
          // Clear the adapter of previous earthquake data
          clearAdapter();
  
          // If there is a valid list of Shows, then add them to the adapter's
          // data set. This will trigger the ListView to update.
          if (shows != null &amp;&amp; !shows.isEmpty()) {
              mShowAdapter.addItemsToList(shows, false);
              mShowAdapter.notifyDataSetChanged();
          }
  
          pb.setVisibility(View.GONE);
      }
  
      @Override
      public void onLoaderReset(Loader&lt;List&lt;Show&gt;&gt; loader) {
          // Loader reset, so we can clear out our existing data.
          clearAdapter();
      }
  
  
      private void clearAdapter(){
          List&lt;Show&gt; empty = new ArrayList&lt;&gt;();
          mShowAdapter.addItemsToList(empty, false);
          mShowAdapter.notifyDataSetChanged();
      }
  </code></pre>
  
  <p>Thanks</p>