2010-10-17 25 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

相关问题