2011-11-23 80 views
3

以及我試圖更新autoCompleteTextview ArrayAdapter dynamiclly每次通過使用TextWathcer更改文本。Android - AutoCompleteTextView動態更新問題

每當文本發生變化時,在新的AsyncTask中和來自異步任務(onPostExecute)的回調中,我從適配器調用clear()並向它添加新項目,然後調用notifyDataSetChanged。 不幸的是,自動完成下拉是從來沒有顯示..

請,我在哪裏去worng?

這裏是代碼:

AutoCompleteTextView acCountry = (AutoCompleteTextView)layout.findViewById(R.id.autoComplete); 
final ArrayAdapter<RMAutoComplete.ListItem> countriesAdapter = new ArrayAdapter<RMAutoComplete.ListItem>(this.context,android.R.layout.simple_dropdown_item_1line); 
acCountry.addTextChangedListener(new TextWatcher() { 

      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       if (s.length() > 1) 
       { 
        new AsyncTask<String, Void, List<RMAutoComplete.ListItem>>(){ 

         @Override 
         protected List<ListItem> doInBackground(String... params) { 
          List<ListItem> l = null; 
          try { 
           l = location.getCountryData(params[0]); 
          } catch (Exception e) { 
           Log.e(TAG,"error when getCountryData",e); 
          } 
          return l; 
         } 

         @Override 
         protected void onPostExecute(List<ListItem> countries) { 
          countriesAdapter.clear(); 
          if (countries != null) 
           for (ListItem listItem : countries) { 
            countriesAdapter.add(listItem); 
           } 
          countriesAdapter.notifyDataSetChanged(); 
         } 
        }.execute(s.toString()); 
       } 
      } 

      public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

      public void afterTextChanged(Editable s) {} 
     } 
    ); 
+0

我正在做類似的事情! http://stackoverflow.com/questions/12854336/autocompletetextview-backed-by-cursorloader – toobsco42

回答

3

常見錯誤:你將適配器設置爲acCountry呢?

+1

謝謝你!我不能相信這是我的錯誤.. 3小時!常見的錯誤。 :) – Adler