title: Android - Searching a fragment
body: <p>Having some trouble searching an already created fragment instance. The problem is that I can only extend fragment, not listfragment etc.</p>
  
  <p>I've tried onQueryTextListener and other methods, but I'm just lost right now.
  I created a simple arraylist that's displayed. All I want to do is be able to search through the arraylist and display items based on text. I already have the xml file, it's just a simple one with a search view.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>public class SearchListFragment extends Fragment {
  
  View games;
  public SearchView search;
  public ListView searchResults;
  
  public static final String ARG_ITEM_ID = "Search list";
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      ViewGroup rootView = (ViewGroup) inflater
              .inflate(R.layout.fragment_search, container, false);
  
      setHasOptionsMenu(true);
  
      ListView phoneListView = ((ListView) rootView.findViewById(R.id.games));
  
      ArrayList&lt;String&gt; gameList = new ArrayList&lt;String&gt;();
      gameList.add("Bloodborne: The Old Hunters");
      gameList.add("Kingdom Hearts III");
      gameList.add("Final Fantasy XV");
  
      ArrayAdapter&lt;String&gt; arrayAdapter =
              new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.simple_list_item_1, gameList);
      // Set The Adapter
      phoneListView.setAdapter(arrayAdapter);
      return rootView;
  
    }
  }
  </code></pre>
  
  <p>Should I not extend fragment and extend listfragment and just try something new? I'm completely lost. The end result should just be a searchview, or even an on click search button, that prompts the user to type or click text, then refreshes the fragment page based on what they typed.</p>
  
  <p>Sorry if the code contains irrelevant information, been messing around with a bunch of views and onclick methods.</p>
  
  <p>I tried adding:</p>
  
  <pre><code>search = (SearchView) rootView.findViewById(R.id.Searcher);
      search.setQueryHint("Start typing to search");
  
      searchResults = (ListView) rootView.findViewById(R.id.games);
  
  
      search.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
  
  
          @Override
          public void onFocusChange(View v, boolean hasFound) {
  
          }
      });
  
      search.setOnQueryTextListener(new OnQueryTextListener() {
          @Override
          public boolean onQueryTextSubmit(String query) {
              return false;
          }
  
          @Override
          public boolean onQueryTextChange(String newText) {
              if (newText.length() &gt; 2) {
                      //todo, would display if text is more than 2 letters
              }
              return false;
          }
      });
  </code></pre>
  
  <p>Which successfully brings up the text search (not the issue), but how do I refresh the fragment based on a search done on the arraylist?</p>