Android Searchview offer from the web

I have a SearchView in the action bar and I want the sentence to be retrieved from the service.

I saw a sample in the Android Samples Searchable Dictionary . but it loads the results from the local database.

I realized that I needed to use a Content Provider . but I am wondering when to get the result from the web service. I mean when you need to make a network call. Please help someone.

+8
android android-searchmanager
source share
1 answer

So, if you use searchview in your project and want to show suggestions, you can implement two methods there

 /** * Called when the user submits the query. This could be due to a key press on the * keyboard or due to pressing a submit button. * The listener can override the standard behavior by returning true * to indicate that it has handled the submit request. Otherwise return false to * let the SearchView handle the submission by launching any associated intent. * * @param query the query text that is to be submitted * * @return true if the query has been handled by the listener, false to let the * SearchView perform the default action. */ boolean onQueryTextSubmit(String query); /** * Called when the query text is changed by the user. * * @param newText the new content of the query text field. * * @return false if the SearchView should perform the default action of showing any * suggestions if available, true if the action was handled by the listener. */ boolean onQueryTextChange(String newText); 

As you can see from the documentation, how these methods work. Thus, while the user enters the onQueryTextChange method, it is also called every time a new keyword is entered or deleted, it will be called.

In this method, you can call the server to receive offers and give it to the user.

Now, if the user wants to search, for example. "apple", so it makes no sense to hit the server for each keyword, for example, for server "a", and then "ap" after this "application", etc.,

therefore, it would be better to attack the server as soon as the user stops typing in the search box, which you can handle using the handler and doing something like this.

  Handler mHandler; String mQueryText; @Override public boolean onQueryTextChange(final String newText) { mQueryText=newText; //Make if invisible once you get the data. mProgressBar.setVisibility(View.VISIBLE); mHandler.removeCallbacks(mRunnable); mHandler.postDelayed(mRunnable,200); return true; } private Runnable mRunnable =new Runnable() { @Override public void run() { //Hit server here. } }; 

While you can hit the server and receive data, you can show a progress bar or something else.

You can apply the same logic to EditText https://developer.android.com/reference/android/widget/EditText.html . Hope this helps.

0
source share

All Articles