title: How to implement search in Recycler View properly
body: <p>i have a RecyclerView that is taking the data using Realm database, i want to implement a SearchView on the action bar that can search through the items of the RecyclerView, this is my code but i can't seem to make it work.</p>
  
  <p>here's the menu XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
  &lt;item android:id="@+id/action_search"
      android:icon="@drawable/ic_search_white_24dp"
      android:title="Search"
      app:actionViewClass="android.support.v7.widget.SearchView"
      app:showAsAction="ifRoom"/&gt;
  &lt;item android:id="@+id/action_add"
      android:icon="@drawable/ic_add_white_48dp"
      android:title="Add"
      app:showAsAction="ifRoom"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>the onOptionsItemSelected at the activity</p>
  
  <pre><code>public boolean onOptionsItemSelected(MenuItem item) {
      // Take appropriate action for each action item click
      switch (item.getItemId()) {
          case R.id.action_search:
              // search action
              SearchView searchView=(SearchView)item.getActionView();
              searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                  @Override
                  public boolean onQueryTextSubmit(String query) {
                      //adapter.getFilter().filter(query);
                      return false;
                  }
  
                  @Override
                  public boolean onQueryTextChange(String newText) {
                      //adapter.getFilter().filter(newText);
                      newText=newText.toLowerCase();
                      System.out.println(results);
                      ArrayList&lt;UserInfo&gt; newList=new ArrayList&lt;&gt;();
                      for (UserInfo userInfo : results){
                          String username=userInfo.getUsername().toLowerCase();
                          String password=userInfo.getPassword().toLowerCase();
                          String type=userInfo.getType().toLowerCase();
                          if (username.contains(newText)||password.contains(newText)||type.contains(newText)){
                              newList.add(userInfo);
                          }
                      }
                      adapter.setFilter(newList);
                      return true;
                  }
              });
              return true;
          case R.id.action_add:
              addInfo();
              return true;
          default:
              return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>my Adapter class</p>
  
  <pre><code>public class Adapter extends RecyclerView.Adapter&lt;Adapter.MyViewHolder&gt; {
  private LayoutInflater layoutInflater;
  java.util.List&lt;UserInfo&gt; data= Collections.emptyList();
  ArrayList&lt;UserInfo&gt; arrayList;
  public Adapter(Context context, java.util.List&lt;UserInfo&gt; data){
      layoutInflater=LayoutInflater.from(context);
      this.data=data;
  }
  
  @Override
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view=layoutInflater.inflate(R.layout.list_item , parent,false);
      MyViewHolder holder=new MyViewHolder(view);
      return holder;
  }
  
  @Override
  public void onBindViewHolder(MyViewHolder holder, int position) {
      UserInfo current=data.get(position);
      holder.username.setText("Username: "+current.username);
      holder.password.setText("Password: "+current.password);
      holder.type.setText("Type: "+current.type);
  }
  
  @Override
  public int getItemCount() {
      return data.size();
  }
  
  class MyViewHolder extends RecyclerView.ViewHolder{
      TextView username,password,type;
      public MyViewHolder(View itemView) {
          super(itemView);
          username= (TextView) itemView.findViewById(R.id.username_TV);
          password= (TextView) itemView.findViewById(R.id.password_TV);
          type= (TextView) itemView.findViewById(R.id.type_TV);
      }
  }
  public void setFilter(List&lt;UserInfo&gt; newList){
      arrayList=new ArrayList&lt;&gt;();
      arrayList.addAll(newList);
      notifyDataSetChanged();
  }
  </code></pre>
  
  <p>}</p>