title: How to clear all items in a ListView while using List Adapter onTextChange?
body: <p>I have been trying to find answers, but it has been hard to find a solution that works. </p>
  
  <p>I tried setting the adapter to null, clearing the actual list but neither seems to work.</p>
  
  <p>I am using a ListView with a ListAdapter and am trying to make it clear on a change of search Text when text is changed.</p>
  
  <p>list.clear(); works but it does not occur on text change.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    private EditText search_input;
          private Button search_button;
  
          // progress bar for search results
          private ProgressDialog search_loading;
  
          private ListView wordSearchList;
          private ListAdapter adapter;
  
      // no result layout
          private LinearLayout no_res;
  
          // create list for adapter
          ArrayList&lt;HashMap&lt;String, String&gt;&gt; list;
          // database helper
          private DatabaseHelper db;
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.dictionary_search);
  
              search_input = (EditText) findViewById(R.id.search_dictionary);
              search_button = (Button) findViewById(R.id.search_button);
                  search_button.setOnClickListener(this);
      // linear layout for no results
              no_res = (LinearLayout) findViewById(R.id.search_result_ll);
  
              // create hashmap list
              list = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
  
          // remove views if they exist
              search_input.addTextChangedListener(new TextWatcher() {
  
                  @Override
                  public void onTextChanged(CharSequence s, int start, int before,
                          int count) {
  
                          // REMOVE LIST VIEW AND ADAPTER
                  // list.clear();
                  if (no_res.getChildCount() &gt; 0) {
                      no_res.removeAllViews();
                  }
  
                  }
  
                  @Override
                  public void beforeTextChanged(CharSequence s, int start, int count,
                          int after) {
                  }
  
                  @Override
                  public void afterTextChanged(Editable s) {
                  }
              });
  
          }
  
      @Override
          public void onClick(View v) {
              if (v == search_button) {
      // clear list for fresh start
              list.clear();
              no_res.removeAllViews();
  
                  // validate input and that something was entered
                  if (search_input.getText().toString().length() &lt; 1) {
  
                      // missing required info (null was this but lets see)
                      Toast.makeText(getApplicationContext(),
                              "Please search for something!", Toast.LENGTH_LONG)
                              .show();
  
                  } else {
  
                      String search_data;
                      search_data = search_input.getText().toString();
  
                      // remove any current views on search again
                      // REMOVE THE LIST VIEW
  
                      // execute the query search
                      List&lt;DatabaseWordsFTS&gt; search_results = db
                              .getSingleWordSearch(search_data);
                      // if no search results returned
                      if (search_results.size() &lt;= 0) {
                          TextView no_results_tv = new TextView(this);
                      no_results_tv.setText("No results found.");
  
                      no_res.addView(no_results_tv);
  
  
  
  
      }
  
  // setup listview
                  wordSearchList = (ListView) findViewById(R.id.wordSearchList);
  
                  for (DatabaseWordsFTS word_found : search_results) {
  
                      // have to create hashmap in loop
                      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
                      // convert d id to long
                      Integer dictionary_id_convert = (int) (long) word_found._dictionaryId;
  
                      // extract dictionary from d-id - since it is not a list and
                      // just a variable
                      DatabaseDictionary dictionary_found = db
                              .getDictionary(dictionary_id_convert);
  
                      // extract languages to send below
                      Integer dln_1 = (int) dictionary_found._language1Id;
                      Integer dln_2 = (int) dictionary_found._language2Id;
                      Integer dln_3 = (int) dictionary_found._language3Id;
                      Integer dln_4 = (int) dictionary_found._language4Id;
  
                      // get languages for the words based on ids passed in
                      List&lt;DatabaseLanguages&gt; LanguagesForD = db
                              .getAllLanguagesWithId(dln_1, dln_2, dln_3, dln_4);
  
                      // add name to hashmap and rest of the data as strings
                      map.put("w_1", word_found.get_word1_fts());
                      map.put("l_1", LanguagesForD.get(0)._language_name);
                      map.put("d_id", String.valueOf(dictionary_id_convert));
                      map.put("w_id", String.valueOf(word_found.get_id()));
  
                      if (word_found.get_word2_fts() != null) {
                          map.put("w_2", word_found.get_word2_fts());
                          map.put("l_2", LanguagesForD.get(1)._language_name);
                      }
  
                      if (word_found.get_word3_fts() != null) {
                          map.put("w_3", word_found.get_word3_fts());
                          map.put("l_3", LanguagesForD.get(2)._language_name);
                      }
                      if (word_found.get_word4_fts() != null) {
                          map.put("w_4", word_found.get_word4_fts());
                          map.put("l_4", LanguagesForD.get(3)._language_name);
                      }
  
                      list.add(map);
  
                      // used to dismiss progress bar for searching
                      search_loading.dismiss();
                  }
  
                  String[] from = { "w_1", "w_2", "w_3", "w_4" }; // , "word3",
                                                                  // "word4"
                  int[] to = { R.id.textName, R.id.textLanguage };
                  adapter = new SimpleAdapter(this, list,
                          R.layout.dictionary_row, from, to);
  
                  wordSearchList.setAdapter(adapter);
                  wordSearchList
                          .setOnItemClickListener(new AdapterView.OnItemClickListener() {
  
                              @Override
                              public void onItemClick(AdapterView&lt;?&gt; parent,
                                      View view, int position, long id) {
  
                                  // ListView Clicked item index
                                  int itemPosition = position;
  
                                  // ListView Clicked item value
                                  HashMap itemValue = (HashMap) wordSearchList
                                          .getItemAtPosition(position);
  
                                  String w_id = (String) itemValue.get("w_id");
                                  String d_id = (String) itemValue.get("d_id");
                                  String l_1 = (String) itemValue.get("l_1");
                                  String l_2 = (String) itemValue.get("l_2");
                                  String l_3 = (String) itemValue.get("l_3");
                                  String l_4 = (String) itemValue.get("l_4");
                                  String w_1 = (String) itemValue.get("w_1");
                                  String w_2 = (String) itemValue.get("w_2");
                                  String w_3 = (String) itemValue.get("w_3");
                                  String w_4 = (String) itemValue.get("w_4");
  
                                  // Show Alert
                                  Toast.makeText(
                                          getApplicationContext(),
                                          "Position :" + itemPosition
                                                  + "  ListItem : " + w_id,
                                          Toast.LENGTH_LONG).show();
  
                                  // creating bundle
                                  Bundle d_data = new Bundle();
  
                                  // add to bundle
                                  d_data.putString("w_id", w_id);
                                  d_data.putString("wd_id", d_id);
                                  d_data.putString("w_1", w_1);
                                  d_data.putString("l_1", l_1);
  
                                  // get tags only if it exists
                                  if (w_2 != null) {
                                      d_data.putString("w_2", w_2);
                                      d_data.putString("l_2", l_2);
                                  }
  
                                  if (w_3 != null) {
                                      d_data.putString("w_3", w_3);
                                      d_data.putString("l_3", l_3);
                                  }
  
                                  if (w_4 != null) {
                                      d_data.putString("w_4", w_4);
                                      d_data.putString("l_4", l_4);
                                  }
  
                                  // start new intent based on the tag -
                                  Intent single_word_view = new Intent(
                                          DictionaryWordSearch.this,
                                          DictionarySingleWordView.class);
  
                                  // call extras
                                  single_word_view.putExtras(d_data);
  
                                  // new_dictionary_view.putExtra("d_id",
                                  // WhatAmISupposeToPassInHere);
                                  startActivity(single_word_view);
  
                              }
  
                          });
              }
  </code></pre>
  
  <p>EDIT: (Below worked for me)</p>
  
  <p>Changed ListAdapter to SimpleAdapter</p>
  
  <pre><code>if(adapter != null){list.clear(); adapter.notifyDataSetChanged();} 
  </code></pre>
  
  <p>Added the above code in onTextChange</p>