How to improve performance of google places autocomplete suggestions?

怎甘沉沦 提交于 2019-12-04 09:58:24

Replace your "addTextChangedListener" method of autocompleteTextView with following code...

edit_destination.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
        return false;
    }

});
edit_destination.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {   
        // Creating a DownloadTask to download Google Places matching "s"

        if(placesDownloadTask!=null)
        {                        
              Log.i("--placesDownloadTask--","progress_status : "+placesDownloadTask.getStatus());
              placesDownloadTask.cancel(true);      
        }


    }

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

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub      
        String chterm; 
        chterm=edit_destination.getText().toString();
        Log.i("---final selected text---", ""+chterm);
        placesDownloadTask = new DownloadTask(PLACES);

        // Getting url to the Google Places Autocomplete api
        String url = getAutoCompleteUrl(s.toString());

        // Start downloading Google Places
        // This causes to execute doInBackground() of DownloadTask class
        placesDownloadTask.execute(url);
    }
});

Instead of giving call from onTextChanged, give call from afterTextChanged it reduces the number of calls after each character and hence reduces the delays.

Try it out, It may help you lot.

There is another method, as above one didn't work for me.
Replace your 'addTextChangedListener' with this one.

This will create a new timer every time it executes the onTextChanged() method and cancels the earlier assigned timertask.

edit_destination.addTextChangedListener(new TextWatcher() {

    Timer timer = new Timer();
    int DELAY = 3000;
    String chterm;
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Cancel the timer if already running. 
            timer.cancel();
            chterm = s.toString();

            // (Optional) Check if length of query is greater than 3
            if(s.length() >= 3) {
                // Start a new timer and assign a TimerTask to it.  
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        Log.i("---final selected text---", "" + chterm);
                        // Getting url to the Google Places Autocomplete api
                        String url = getAutoCompleteUrl(chterm);
                        // Creating a DownloadTask to download Google Places matching "s"
                        placesDownloadTask = new DownloadTask(PLACES);
                        // Start downloading Google Places
                        // This causes to execute doInBackground() of DownloadTask class
                        placesDownloadTask.execute(url);
                    }
                }, DELAY);
            }
        }

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

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub              
    }
}); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!