AutoCompleteTextView doesn't show dropdown when I press space after typing a complete word

前端 未结 4 872
囚心锁ツ
囚心锁ツ 2020-12-17 16:04

My main activity code:

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


        
4条回答
  •  遥遥无期
    2020-12-17 16:38

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

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

    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 adapter1;
    //... 
    adapter1 = new FilterWithSpaceAdapter(MainActivity.this,
                android.R.layout.simple_dropdown_item_1line, dataArray);
    

提交回复
热议问题