2014-02-20 28 views
0

我一直在努力尋找答案,但很難找到有效的解決方案。如何在使用List Adapter onTextChange時清除ListView中的所有項目?

我嘗試將適配器設置爲null,清除實際列表,但似乎都不起作用。

我正在使用帶有ListAdapter的ListView,並試圖在更改文本時更改搜索文本。

list.clear();工作但它不會發生在文本更改。

這裏是我的代碼:

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<HashMap<String, String>> 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<HashMap<String, String>>(); 


     // 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() > 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() < 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<DatabaseWordsFTS> search_results = db 
          .getSingleWordSearch(search_data); 
        // if no search results returned 
        if (search_results.size() <= 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<String, String> map = new HashMap<String, String>(); 

        // 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<DatabaseLanguages> 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<?> 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); 

          } 

         }); 
      } 

編輯:(以下爲我工作)

更改ListAdapter到SimpleAdapter

if(adapter != null){list.clear(); adapter.notifyDataSetChanged();} 

添加上面的代碼中onTextChange

+3

使用適配器。 clear()和Adapter.notifyDataSetChanged() –

+0

不適用於列表適配器 – Lion789

回答

0

看如果你想TextView沒有你可以實現的結果這個代碼

listView.setEmptyView(emptyView) 

,並通過您的TextView這種方法,

用於清除與空的ListView可以清除您的收藏和呼叫notifyChangeDataSet或者設置適配器都試一下,並餵我回來

+0

noResults我得到它與一個不同的方法工作,所以很好,只是該列表是textChanged沒有清除,這是我只有 – Lion789

+0

你能發佈更多的代碼,直到我可以測試它嗎? –

+0

嘿,如果你可以編輯你的代碼什麼結束了工作是這一行if(adapter!= null){list.clear(); adapter.notifyDataSetChanged();}然後,我可以標記你的答案是正確的 – Lion789

相關問題