AutoCompleteTextView不显示下拉列表,当我键入$一个完整的字后,p $ PSS空间当我、完整、列表、空间

2023-09-04 11:14:02 作者:能滚多远就滚多远

我的主要活动code:

My main activity code:

// 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();

        }
    });

当我搜索夏尔马和preSS后有一个空格,该建议熄灭。我希望这些建议呆在那里。我试图做的一切,但没有得到任何的成功。是否有人可以帮忙吗?

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?

编辑: 是否有人可以尝试在他们的模拟器这个code?只需添加一个XML格式的 AutoCompleteTextView 并运行它。

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

推荐答案

首先,有没有为什么你设置任何原因, TextWatcher 监听器上的 AutoCompleteTextView ?如果你这样做是为了过滤数据自己,你不应该这样做(因为小部件做到这一点在默认情况下,你的code是不正确的)。

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).

当我搜索夏尔马和preSS后的空间。建议去   关闭。我希望这些建议呆在那里。

仿百度谷歌搜寻自动提示框 AutoCompleteTextView的应用

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

这是发生因为适配器和默认过滤器的实现,它与它来了,元素的 AutoCompleteTextView 使用引擎盖下提供您在下拉列表中看到的值。默认行为为 ArrayAdapter 是一个你看,你可以找到在this回答。该解决方案是,将搜索整个适配器的一行数据过滤字符串过滤器来实现自己的适配器。我已经采取了 ArrayAdapter 类的code从SDK,并提出了小幅调整,以便插入一个单词后的空间时,过滤不破。你可以找到类here为code是往大了后。只需复制类在项目中,并用它作为一个正常的 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);
 
精彩推荐
图片推荐