Autocompletion delay

前端 未结 4 487
予麋鹿
予麋鹿 2020-12-17 06:33

I\'ve got to set an autocompletion for my application.

I\'ve already understood the AutoCompleteTextView operation, but I\'d like to dynamically modify the Stri

4条回答
  •  囚心锁ツ
    2020-12-17 07:25

    Can't comment on the answer selected as correct so here it goes. This solution will only work if you press a key after the specified delay has elapsed. If you write very fast and then stop no request will be sent to the server.

    You can make that happen using a Handler and sending messages delayed:

    public void onTextChanged(CharSequence s, int start, int before,
                               int count) {
    
        final String term = s.toString();
        if( term.length() > 3 ){
            autocompleteHandler.removeMessages(0);
            autocompleteHandler.postDelayed(new Runnable(){
                @Override
                public void run() {
                new SearchLocationAsyncTask(context, term).execute();
            }}, 350);
        }
    }
    

    Where SearchLocationAsynTask calls your server and updates the view accordingly.

    Basically what this does is add a new message to the handler everytime the text changes, if there are any messages in the queue they get deleted and a new one is added to execute in 350ms.

提交回复
热议问题