5

我在嘗試使用和ArrayAdapter動態更新AutocompleteTextView的列表。爲了更新此視圖,我使用TextWatcher來監視AutocompleteTextView中可能發生的任何更改。帶有ArrayAdapter和TextWatcher的動態AutocompleteTextView

問題是,列表並沒有更新,我不明白爲什麼。我一直在尋找類似於互聯網的東西,我已經找到了幾種不同的方法,但我仍然不明白爲什麼這個應該是最簡單的方法不起作用。任何解釋將不勝感激。

目標AVD:谷歌API的10級的Android 2.3.3

下面是簡化的代碼:

public class AutocompleteActivity extends Activity implements TextWatcher { 
    ArrayAdapter<String> adapter = null; 
    AutoCompleteTextView acTextView = null; 
    ArrayList<String> addresses = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.autocompleteview); 

     acTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete_address); 
     adapter = new ArrayAdapter<String>(this, R.layout.listitem, addresses); 
     adapter.setNotifyOnChange(true); 
     acTextView.addTextChangedListener(this); 
     acTextView.setAdapter(adapter); 
    } 

    @Override 
    public void onTextChanged(CharSequence text, int start, int before, int after) { 
     try { 
      adapter.add("test"); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
+0

nvm,它的工作原理:-( – gioski

+0

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

回答

3

在你的源代碼,TextWatcher提供給AutoCompleteTextView,這纔是真正的問題。

如果你看看AutoCompleteTextView的源代碼,你會發現AutoCompleteTextView有自己的名爲「MyWatcher」的TextWatcher。因此,AutoCompleteTextView無法響應您的輸入操作。

相關問題