【问题标题】:Getting current suggestion from `AutoCompleteTextView`从“AutoCompleteTextView”获取当前建议
【发布时间】:2010-10-17 16:57:43
【问题描述】:

您如何获得AutoCompleteTextView 中的当前最佳建议?我有它建议项目,并且我注册了一个文本更改侦听器。我在同一个屏幕上也有一个列表。当他们键入时,我想将列表滚动到当前的“最佳”建议。但我不知道如何访问当前的建议,或者至少是最重要的建议。我想我正在寻找类似AutoCompleteTextView.getCurrentSuggestions():

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
        }
    });

【问题讨论】:

    标签: android autocomplete


    【解决方案1】:

    我已经用下面的代码完成了你想做的事情:

    @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

    【讨论】:

    • 很好的答案!比我下面的 hack 好多了。
    • 非常感谢您。我尝试了几天来弄清楚如何根据过滤项目的数量动态更改下拉列表的高度,并且它似乎在 afterTextChanged 事件之后过滤。 dataSetObserver 正是我需要让它工作的监听器
    【解决方案2】:

    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;
    

    【讨论】:

    • 我测试了你的代码,但在“lastResults = (ArrayList) results”中,但我们无法将 FilterResults 类型转换为 ArrayList
    • 你能告诉我如何修改吗? sdk2.2
    • 请看上面的答案——它比我的更好;)
    猜你喜欢
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多