title: Implement Search in ListView inside Fragment
body: <p>I am trying to implement search functionality in a listview built into a fragment. The listview is working fine, but when I type on the edittext to search, it disappears.</p>
  <p>My code:</p>
  <pre><code>public class DrinksFragment extends Fragment {
  
      private View rootView;
      private ArrayAdapter&lt;DrinksList&gt; adapter;
      private List&lt;DrinksList&gt; drinks;
      private ListView lv;
      ArrayList&lt;DrinksList&gt; mAllData;
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          rootView = inflater.inflate(R.layout.activity_drinks_fragment, container, false);
          populateDrinksList();
          doSearch();
          return rootView;
      }
  
      private void doSearch() {
          final EditText et = (EditText)rootView.findViewById(R.id.searchListDrinks);
          et.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              }
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  
              }
  
              @Override
              public void afterTextChanged(Editable s) {
                  String text = et.getText().toString().toLowerCase(Locale.getDefault());
              adapter.filter(text);
              }
          });
      }
  
  
  
      private void populateDrinksList() {
          drinks = new ArrayList&lt;DrinksList&gt;();
          drinks.add(new DrinksList(R.drawable.coca, &quot;Coca Cola&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.cocalight, &quot;Coca Cola Light&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.cocazero, &quot;Coca Cola Zero&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.orange, &quot;Fanta Orange&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.lemon, &quot;Fanta Lemon&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.ble, &quot;Fanta Blue&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.sprite, &quot;Sprite&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.soda, &quot;Soda Water&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.tonic, &quot;Tonic Water&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.ioli, &quot;Sparkling Water Ioli&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.perrier, &quot;Sparkling Water Perrier&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.nero, &quot;Still Water&quot;, 2.00));
          drinks.add(new DrinksList(R.drawable.redbull, &quot;Red Bull&quot;, 4.00));
          drinks.add(new DrinksList(R.drawable.zelita, &quot;Zelita&quot;, 2.50));
          lv = (ListView)rootView.findViewById(R.id.drinksListView);
          adapter = new MyCustomDrinksListAdapter(getActivity().getApplicationContext(), R.layout.list_item, drinks);
          lv.setAdapter(adapter);
      }
  
  }public class DrinksFragment extends Fragment {
  
      private View rootView;
      private ArrayAdapter&lt;DrinksList&gt; adapter;
      private List&lt;DrinksList&gt; drinks;
      private ListView lv;
      ArrayList&lt;DrinksList&gt; mAllData;
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          rootView = inflater.inflate(R.layout.activity_drinks_fragment, container, false);
          populateDrinksList();
          doSearch();
          return rootView;
      }
  
      private void doSearch() {
          final EditText et = (EditText)rootView.findViewById(R.id.searchListDrinks);
          et.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              }
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  DrinksFragment.this.adapter.getFilter().filter(s);
              }
  
              @Override
              public void afterTextChanged(Editable s) {
                  
              }
          });
      }
  
  
  
      private void populateDrinksList() {
          drinks = new ArrayList&lt;DrinksList&gt;();
          drinks.add(new DrinksList(R.drawable.coca, &quot;Coca Cola&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.cocalight, &quot;Coca Cola Light&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.cocazero, &quot;Coca Cola Zero&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.orange, &quot;Fanta Orange&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.lemon, &quot;Fanta Lemon&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.ble, &quot;Fanta Blue&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.sprite, &quot;Sprite&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.soda, &quot;Soda Water&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.tonic, &quot;Tonic Water&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.ioli, &quot;Sparkling Water Ioli&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.perrier, &quot;Sparkling Water Perrier&quot;, 2.50));
          drinks.add(new DrinksList(R.drawable.nero, &quot;Still Water&quot;, 2.00));
          drinks.add(new DrinksList(R.drawable.redbull, &quot;Red Bull&quot;, 4.00));
          drinks.add(new DrinksList(R.drawable.zelita, &quot;Zelita&quot;, 2.50));
          lv = (ListView)rootView.findViewById(R.id.drinksListView);
          adapter = new MyCustomDrinksListAdapter(getActivity().getApplicationContext(), R.layout.list_item, drinks);
          lv.setAdapter(adapter);
      }
  
  }
  </code></pre>
  <p>My Adapter:</p>
  <pre><code>public class MyCustomDrinksListAdapter extends ArrayAdapter&lt;DrinksList&gt; {
  
  
      private List&lt;DrinksList&gt; items = null;
      private ArrayList&lt;DrinksList&gt; arraylist;
      public MyCustomDrinksListAdapter(Context context, int layoutId, List&lt;DrinksList&gt; items) {
          super(context, layoutId, items);
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View arrayView = convertView;
          if(arrayView == null){
              LayoutInflater vi;
              vi = LayoutInflater.from(getContext());
              arrayView = vi.inflate(R.layout.list_item, parent, false);
          }
  
          DrinksList currentPosition = getItem(position);
          if(currentPosition != null){
              ImageView image = (ImageView)arrayView.findViewById(R.id.product_image_coffee);
              image.setImageResource(currentPosition.getImage());
  
              TextView name = (TextView)arrayView.findViewById(R.id.product_name_coffee);
              name.setText(currentPosition.getName());
  
              TextView price = (TextView)arrayView.findViewById(R.id.product_price_coffee);
              price.setText(String.format(&quot;%.2f&quot;, currentPosition.getPrice()));
          }
          return arrayView;
      }
  
      public void filter(String charText) {
          charText = charText.toLowerCase(Locale.getDefault());
          items.clear();
          if (charText.length() == 0) {
              items.addAll(arraylist);
          } else {
              for (DrinksList wp : arraylist) {
                  if (wp.getName().toLowerCase(Locale.getDefault())
                          .contains(charText)) {
                      items.add(wp);
                  }
              }
          }
          notifyDataSetChanged();
      }
  }
  </code></pre>
  <p>I tried following the android developers example but I could not.</p>