键入完整单词后按空格时,AutoCompleteTextView 不显示下拉列表 [英] AutoCompleteTextView doesn't show dropdown when I press space after typing a complete word

查看:35
本文介绍了键入完整单词后按空格时,AutoCompleteTextView 不显示下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要活动代码:

// here you put all your data.
String[] dataArray = { "Amit sharma Kumar", "Hisham Kumar Munner",
        "Vineet John Chaturvedi", "Lucky Kumar Verma" };

ArrayList<String> alAutoCompleteList;
AutoCompleteTextView acTV;
ArrayAdapter<String> adapter1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // etAuto = (EditText) findViewById(R.id.etAuto);
    acTV = (AutoCompleteTextView) findViewById(R.id.acTV);
    // Arraylist
    alAutoCompleteList = new ArrayList<String>();
    adapter1 = new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_dropdown_item_1line,     alAutoCompleteList);


    acTV.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            if (acTV.enoughToFilter()) {
                acTV.showDropDown();
                acTV.bringToFront();
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
            alAutoCompleteList.clear();
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

            String acText = acTV.getText().toString().trim();

            for (String item : dataArray) {

                if     (item.toLowerCase().contains(acText.toLowerCase())) {
                    alAutoCompleteList.add(item);
                }
            }


            acTV.setThreshold(4);
            acTV.setAdapter(adapter1);
            acTV.showDropDown();

        }
    });

<小时>

当我搜索sharma"并按空格后,建议就会消失.我希望这些建议留在那里.我试图做所有事情,但没有取得任何成功.有人可以帮忙吗?


When I search for "sharma" and press a space after that the suggestions go off. I want those suggestions to stay there. I have tried to do everything but didn't got any success. Can someone please help?

有人可以在他们的模拟器上试试这个代码吗?只需在 xml 中添加一个 AutoCompleteTextView 并运行它.

Can someone please try this code on their emulators? Just add a AutoCompleteTextView in xml and run it.

推荐答案

首先,您为什么要在 AutoCompleteTextView 上设置 TextWatcher 侦听器?如果您这样做是为了自己过滤数据,则不应这样做(因为小部件默认执行此操作,而您的代码不正确).

First, is there any reason why you set that TextWatcher listener on the AutoCompleteTextView? If you did this to filter the data yourself you shouldn't do it(because the widget does this by default and your code is incorrect).

当我搜索sharma"并在此之后按一个空格时.建议去离开.我希望这些建议留在那里.

When i search "sharma" and press a space after that. suggestions goes off. I want those suggestions to stay there.

这是由于适配器和它附带的默认 Filter 实现,AutoCompleteTextView 在引擎盖下使用的元素来提供您在其中看到的值下拉列表.ArrayAdapter 的默认行为是您看到的行为,您可以在 这个答案.解决方案是使用过滤器实现您自己的适配器,该过滤器将在整个适配器的行数据中搜索过滤器字符串.我从 SDK 中获取了 ArrayAdapter 类的代码并做了一些调整,这样在单词后插入空格时过滤不会中断.您可以在 here 中找到类a> 因为代码太大,无法发布.只需复制项目中的类并将其用作普通的ArrayAdapter:

This is happening because of the adapter and the default Filter implementation which comes with it, elements that the AutoCompleteTextView uses under the hood to provide the values that you see in the drop down list. The default behavior for an ArrayAdapter is the one you see, you can find an explanation in this answer. The solution is to implement your own adapter with a filter that will search the whole adapter's row data for the filter string. I've taken the code of the ArrayAdapter class from the SDK and made a slight adjustment so the filtering doesn't break when inserting a space after a word. You can find the class here as the code is to big to post. Just copy the class in your project and use it as a normal ArrayAdapter:

FilterWithSpaceAdapter<String> adapter1;
//... 
adapter1 = new FilterWithSpaceAdapter<String>(MainActivity.this,
            android.R.layout.simple_dropdown_item_1line, dataArray);

这篇关于键入完整单词后按空格时,AutoCompleteTextView 不显示下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆