-3

So here i need to get the text the user enters into the search view in order to be able to update the GOOGLE_BOOKS_REQUEST_URL,

public class BookListingActivity extends AppCompatActivity implements LoaderCallbacks<List<BookListing>> {

    private static final String LOG_TAG = BookListingActivity.class.getName();

    /**
     * This is the search the user does for the API query
     */
    private String userQuery;
    /**
     * URL for book data from the google books dataset
     */
    private String GOOGLE_BOOKS_REQUEST_URL =
            "https://www.googleapis.com/books/v1/volumes?q=" + userQuery;
    /**
     * Constant value for the book loader ID. We can choose any integer.
     * This really only comes into play if you're using multiple loaders.
     */
    private static final int BOOK_LOADER_ID = 1;

    /**
     * Adapter for the list of books
     */
    private BookListingAdapter mAdapter;

    /**
     * TextView that is displayed when the list is empty
     */
    private TextView mEmptyStateTextView;

    /**
     * ProgressBar that is displayed when the list is loading
     */
    private ProgressBar mProgressBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Find the searchView in which the query will be performed
        SearchView query = (SearchView) findViewById(R.id.search_button);
        query.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
Balasubramanian
  • 700
  • 7
  • 26
  • This might help https://stackoverflow.com/questions/14426769/how-to-change-android-searchview-text – abhi Sep 20 '17 at 08:26
  • I'm assuming you will want to use the callbacks `onQueryTextSubmit` and `onQueryTextChange` – PPartisan Sep 20 '17 at 08:37

1 Answers1

0

You'd want to have something like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Find the searchView in which the query will be performed
        SearchView query = (SearchView) findViewById(R.id.search_button);
        query.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                String url = GOOGLE_BOOKS_REQUEST_URL + query;
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                getLoaderManager().restartLoader(BOOK_LOADER_ID, bundle, BookListingActivity.this);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

Which means that you need to update your GOOGLE_BOOKS_REQUEST_URL variable, it should not contain the query string (since it's empty at first, it makes no sense to have it there).

private static final String GOOGLE_BOOKS_REQUEST_URL = "https://www.googleapis.com/books/v1/volumes?q=";
Husayn Hakeem
  • 4,184
  • 1
  • 16
  • 31
  • I really appreciate the effort, but the problem is that i'm using a loader to handle the request on a background thread. so i guess i need a way to update the variable and then pass it in to the onCreateLoader method. or am i just dumb and the answer is obvious? – Leander vd Walt Sep 20 '17 at 12:34
  • I just noticed in you're code that your Activity implements LoaderManager.LoaderCallBacks. In this case you'd need to restart your loader each time another query is entered. For this you'll need to call restartLoader. I modified the code I wrote. Let me know if this is what you're trying to do. – Husayn Hakeem Sep 20 '17 at 13:25
  • One last question, what do i pass in here now? @Override public Loader> onCreateLoader(int i, Bundle bundle) { // Create a new loader for the given URL return new BookLoader(this, GOOGLE_BOOKS_REQUEST_URL); } – Leander vd Walt Sep 20 '17 at 14:53
  • @LeandervdWalt What you can return is an AsyncTaskLoader for example. It will be in charge of getting data from the url in the background, and returning it on the main thread for you to use. Here's a more detailed stackoverflow post explaining the same as what you want to do https://stackoverflow.com/questions/41267446/how-to-get-loadermanager-initloader-working-within-a-fragment – Husayn Hakeem Sep 21 '17 at 08:34
  • Husayn, Thanks so much buddy, you really helped :D – Leander vd Walt Sep 23 '17 at 11:48