Android searchview's suggestion from the network
Asked Answered
C

1

8

I have a SearchView in the Actionbar and I want the suggestion to be fetched from a service.

I have seen a sample in the android Samples Searchable Dictionary. but it is loading the results from a local database.

I got that, it is necessary to use the Content Provider. but I am wondering when to fetch the result from the web service. I mean when to made a network call. Please can anyone help.

Collector answered 24/12, 2012 at 8:29 Comment(0)
S
0

So if you are using a searchview in you project and want to show the suggestions you can implement there two methods

/**
         * 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. So as long as user is typing onQueryTextChange method will be called and every time new keyword is entered or deleted it will be called.

In this method you can call the server to get the suggestions and give that to user.

Now if user wants to search for eg. "apple" so it won't make sense to hit server for every keyword, like hit server for "a" then "ap" after that "app" and so on,

so it would be better to hit server once user stops typing in the searchview which you can handle using handler and runnable 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 hitting the server and getting the data you can show progress bar or something.

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

Saulsauls answered 25/7, 2017 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.