2010-10-17 88 views
7

如何獲得AutoCompleteTextView中的當前最佳建議?我有建議項目,我有一個文本更改偵聽器註冊。我也在同一個屏幕上有一個列表。當他們鍵入時,我想將列表滾動到當前的「最佳」建議。但我無法弄清楚如何訪問當前的建議,或至少是最重要的建議。我想我正在尋找類似​​:從「AutoCompleteTextView」獲取當前建議

autoCompleteTextView.addTextChangedListener(new TextWatcher() { 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
      String currentText = autoCompleteTextView.getText(); 
      String bestGuess = autoCompleteTextView.getCurrentSuggestions()[0]; 
      //          ^^^ mewthod doesn't exist 
      doSomethingWithGuess(bestGuess); 
     } 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // do nothing 
     } 
     public void afterTextChanged(Editable s) { 
      // do nothing 
     } 
    }); 

回答

15

我已經完成了以下代碼的工作:

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

    adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_dropdown_item_1line, COUNTRIES); 
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit); 
    textView.setAdapter(adapter); 

    adapter.registerDataSetObserver(new DataSetObserver() { 
     @Override 
     public void onChanged() { 
      super.onChanged(); 
      Log.d(TAG, "dataset changed"); 
      Object item = adapter.getItem(0); 

      Log.d(TAG, "item.toString "+ item.toString()); 
     } 
    }); 
} 

item.toString將打印顯示在第一項上的文本。

請注意,即使您沒有顯示彈出窗口(建議),也會發生這種情況。此外,您應該檢查是否有任何項目通過了過濾條件(即用戶的輸入)。

爲了解決第一個問題:

int dropDownAnchor = textView.getDropDownAnchor(); 
    if(dropDownAnchor==0) { 
     Log.d(TAG, "drop down id = 0"); // popup is not displayed 
     return; 
    } 
    //do stuff 

要解決的第二個問題,使用getCount將> 0

+0

很好的答案!比我下面的黑客好得多。 – 2010-10-26 07:17:28

+0

非常感謝你。我正在嘗試幾天,想知道如何根據過濾項目的數量動態更改我的下拉菜單的高度,並且它似乎是在'afterTextChanged'事件之後過濾的。 dataSetObserver正是我需要的偵聽器,它使它工作 – Mike 2013-01-28 15:57:46

+0

這是一個現貨! – 2013-09-03 07:30:45

1

AutoCompleteTextView不向下滾動到最好的選擇,但縮小了您鍵入的選擇。下面是它的一個例子:http://developer.android.com/resources/tutorials/views/hello-autocomplete.html

正如我從AutoCompleteTextView看到它沒有辦法得到當前的建議列表。

唯一的方法似乎是編寫自定義版本的ArrayAdapter並將其傳遞給AutoCompleteTextView.setAdapter(..)。這裏是source to ArrayAdapter。您只能更改內部類ArrayFilter.performFiltering(方法),使其暴露FilterResults:

...添加字段內部類ArrayFilter:

public ArrayList<T> lastResults; //add this line 

..方法performFiltering月底前:

lastResults = (ArrayList<T>) results; // add this line 
    return results; 
} 

使用像這樣(來自鏈路適配示例):

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country); 
CustomArrayAdapter<String> adapter = new CustomArrayAdapter<String>(this, R.layout.list_item, COUNTRIES); 
textView.setAdapter(adapter); 

// read suggestions 
ArrayList<String> suggestions = adapter.getFilter().lastResult; 
+0

我測試你的代碼,但在 「lastResults =(ArrayList的)的結果」,但我們不能轉換一個FilterResults類型爲ArrayList 。 – pengwang 2010-10-26 01:12:46

+0

你能告訴我如何修改? sdk2.2 – pengwang 2010-10-26 01:21:30

+2

請看上面的答案 - 這是比我的更好的解決方案;) – 2010-10-26 07:16:49