title: Searchview in actionbar in fragment
body: <p>I am creating an android application. In the application I have a login page. After login a userlist is displayed. The userlist activity is <code>HomeActivity.java</code> that contains two fragments named as <code>UserListFragment.java</code> and <code>ChatFragment.java</code> The userlist is getting displayed from <strong>firebase</strong>.</p>
  
  <p>The UserListFragment.java contains an actionbar which has a search menu and logout menu. The logout menu works fine but the search isn't working. The search icon gets displayed but when I search something it doesn't show anything i.e. no filters. I am using firebase to display the userlist.</p>
  
  <p>I don't want the searchview to be displayed in ChatFragment.java</p>
  
  <p>I tried various various various solutions. But none worked for me. </p>
  
  <p>Here is the code.</p>
  
  <p><strong>UserListFragment.java</strong></p>
  
  <pre><code>public class UsersListFragment extends Fragment {
  // TODO: Rename parameter arguments, choose names that match
  // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  private static final String ARG_PARAM1 = "param1";
  private static final String ARG_PARAM2 = "param2";
  Activity activity;
  // TODO: Rename and change types of parameters
  private String mParam1;
  private String mParam2;
  private static final String TAG = "UsersListFragment";
  private OnFragmentInteractionListener mListener;
  private ListView listView;
  private List&lt;UserData&gt; users;
  private CustomAdapter adapter;
  SharedPreferences.Editor preferenceEditor;
  Timer myTimer;
  View view;
  ActionBar actionBar;
  private static final String PREFRENCES_NAME = "setPreferences";
  private ProgressDialog progressBar;
  String partnerKeyValue;
  
  // TODO: Rename and change types and number of parameters
  public static UsersListFragment newInstance(String param1, String param2) {
      UsersListFragment fragment = new UsersListFragment();
      Bundle args = new Bundle();
      args.putString(ARG_PARAM1, param1);
      args.putString(ARG_PARAM2, param2);
      fragment.setArguments(args);
      return fragment;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
     Global.getInstance().unreadMessageUsers.clear();
      progressBar = new ProgressDialog(getActivity());
      progressBar.setCancelable(false);
      progressBar.setMessage("Loading...");
      progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
      progressBar.setProgress(0);
      }
  
  @Override
  public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
  
      inflater.inflate(R.menu.menu_userlist,menu);
      MenuItem item = menu.findItem(R.id.menuSearch);
  
      SearchView searchView = (SearchView)item.getActionView();
  
      searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
          @Override
          public boolean onQueryTextSubmit(String query) {
              return false;
          }
  
          @Override
          public boolean onQueryTextChange(String newText) {
              Log.i(TAG,newText);
              adapter.getFilter().filter(newText);
              return true;
          }
      });
  
      super.onCreateOptionsMenu(menu,inflater);
  }
  
  
  private void logoutUser(){
      Intent I = new Intent(getActivity(), LoginActivity.class);
      startActivity(I);
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
  
      switch (item.getItemId()) {
          case R.id.menuSearch :
              return true;
  
          case R.id.menuLogout :
              logoutUser();
              return true;
  
          default:
              return super.onOptionsItemSelected(item);
      }
  
  }
  </code></pre>
  
  <p><strong>menu_userlist.xml</strong></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/menuSearch"
      android:title="@string/search"
      android:icon="@drawable/ic_search"
      app:actionViewClass="android.widget.SearchView"
      app:showAsAction="always"&gt;
  &lt;/item&gt;
  
  &lt;item android:id="@+id/menuLogout"
      android:title="@string/logout"
      android:icon="@drawable/ic_logout"
      app:showAsAction="always"&gt;
  &lt;/item&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>CustomAdapter.java</strong></p>
  
  <pre><code>@Override
  public Filter getFilter() {
      Filter filter = new Filter() {
  
          @SuppressWarnings("unchecked")
          @Override
          protected void publishResults(CharSequence constraint,FilterResults results) {
  
              users = (List&lt;UserData&gt;) results.values; // has the filtered values
              notifyDataSetChanged();  // notifies the data with new filtered values
          }
  
          @Override
          protected FilterResults performFiltering(CharSequence constraint) {
              FilterResults results = new Filter.FilterResults();       
  
              List&lt;String&gt; FilteredArrList = new ArrayList&lt;String&gt;();
  
                  if (mOriginalValues != null) {
  
                  if (constraint == null || constraint.length() == 0) {
  
                      // set the Original result to return
                      results.count = mOriginalValues.size();
                      results.values = mOriginalValues;
                  } else {
                      constraint = constraint.toString().toLowerCase();
                      for (int i = 0; i &lt; mOriginalValues.size(); i++) {
                          String data = mOriginalValues.get(i).toString();
                          if (data.toLowerCase().contains(constraint.toString())) {
                              FilteredArrList.add(data);
                          }
                      }
                      // set the Filtered result to return
                      results.count = FilteredArrList.size();
                      results.values = FilteredArrList;
                  }
              }
  
              return results;
          }
      };
      return filter;
  }
  }
  </code></pre>