Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Google-like search

I'd like to implement a google-like auto-suggest functionality. What's the best way to do it and are there any components already implemented that can help ?

I'm really bothered by how often requests should be sent. My guess is that a request should be sent after some time passes. So, for instance when user wrights in a new character some sort of timer must be triggered, which, by the end, will trigger a function comparing inputs from the start of the timer and it's ending. Judging on comparison made a decision whether to send or not is taken.Should I use threads for that. Won't it be a resource-intensive execution ?

One more option: make a while(true) Thread, which gets text, sends request and update main thread results view. Though, I'm not sure how it's done in terms of Android.

Thanks for any suggestions.

UPD: I've come up with a simple solution of using AsyncTasks. Each time new SearchInBackgroundTask gets launched it sets a flag that prevents any other instance of SearchInBackgroundTask to be launched unless the original one finishes it's job. After flag is set so it allows for another SearchInBackgroundTask execution the last input string gets processed on conditions if it's not empty and not equal to previously processed.

So, suggest any thoughts on that - is it a fine solution ?

public class AsyncTaskForSearchActivity extends Activity {

    protected boolean _threadIsWorking = false;
    protected String _searchResult;
    protected String _searchString;
    protected String _processedSearchString;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView searchbox = (TextView) findViewById(R.id.searchbox);
        searchbox.addTextChangedListener(_searchboxWatcher);
    }

    protected TextWatcher _searchboxWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override
        public void afterTextChanged(Editable s) {
            _searchString = s.toString();
            if(_threadIsWorking == false){
                _performSearchTask();
            }
        }
    };

    protected Handler _onRetrieveResult = new Handler(){
        public void dispatchMessage(android.os.Message msg) {
            if(_threadIsWorking == false && _searchString.length() > 0 && _processedSearchString != _searchString){ // what's the difference bet equeals and ==
                _performSearchTask();
            }
        };
    };

    protected class SearchInBackgroundTask extends AsyncTask<String, Void, Void>{
        protected String _s;

        @Override
        protected Void doInBackground(String... params) {
            _s = params[0];
            try {
                // httpProvider -> retrieve
                Thread.sleep(6000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            _searchResult = "xxx";
            _threadIsWorking = false;
            _onRetrieveResult.sendEmptyMessage(0);
        }
    }

    protected void _performSearchTask(){
        _threadIsWorking = true;
        _processedSearchString = _searchString;
        Toast.makeText(AsyncTaskForSearchActivity.this, _processedSearchString, 1).show();
        (new SearchInBackgroundTask()).execute(_processedSearchString);
    }
}
like image 359
user1384991 Avatar asked Dec 11 '25 02:12

user1384991


1 Answers

Event-driven programming, coupled with an n-ary tree of "common" search phrases, is one way to approach this.

like image 145
Makoto Avatar answered Dec 12 '25 14:12

Makoto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!