2

How do I put the Loader, onLoadFinish, and onLoadReset into "public void onClick(View view) {"

This is the Code I have in my main activity I need to put my loader that is in the oncreate section into my onclicklistener.

I put the init loader in a public class called "load" and was able to call that, but the other public classes I am having a hard time putting in my onclicklistener.

Any help would be appreciated.

package com.example.android.googlebooks;

import android.app.LoaderManager;
import android.content.Context;
import android.content.Intent;
import android.content.Loader;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class BooksActivity extends AppCompatActivity
        implements LoaderManager.LoaderCallbacks<List<Books>> {

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

    /**
     * URL for books data from the USGS dataset
     */
    private static String GOOGLEBOOKS_REQUEST_URL =
            "https://www.googleapis.com/books/v1/volumes?q=";

    /**
     * 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 BOOKS_LOADER_ID = 1;

    /**
     * Adapter for the list of books
     */
    private BookAdapter mAdapter;
    /** TextView that is displayed when the list is empty */

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(LOG_TAG, "TEST: Book Activity onCreate() called");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_books);

        Button mButton = (Button) findViewById(R.id.button);
        EditText mEdit = (EditText) findViewById(R.id.editText);
        assert mEdit != null;
        final Editable mEditText = mEdit.getText();
        assert mButton != null;
        mButton.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View view) {
                        GOOGLEBOOKS_REQUEST_URL = "https://www.googleapis.com/books/v1/volumes?q=" + mEditText;
load();

                        Toast.makeText(getApplicationContext(),
                                GOOGLEBOOKS_REQUEST_URL, Toast.LENGTH_LONG).show();
                    }
                });

        mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);

        // Find a reference to the {@link ListView} in the layout
        ListView bookListView = (ListView) findViewById(R.id.list);

        // Create a new adapter that takes an empty list of books as input
        mAdapter = new BookAdapter(this, new ArrayList<Books>());

        // Set the adapter on the {@link ListView}
        // so the list can be populated in the user interface
        bookListView.setAdapter(mAdapter);

        bookListView.setEmptyView(mEmptyStateTextView);

        // Set an item click listener on the ListView, which sends an intent to a web browser
        // to open a website with more information about the selected books.
        bookListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                // Find the current Books that were clicked on
                Books currentBooks = mAdapter.getItem(position);

                // Convert the String URL into a URI object (to pass into the Intent constructor)
                Uri bookUri = Uri.parse(currentBooks.getmUrl());

                // Create a new intent to view the books URI
                Intent websiteIntent = new Intent(Intent.ACTION_VIEW, bookUri);

                // Send the intent to launch a new activity
                startActivity(websiteIntent);
            }
        });}

    public void load() {

        // Get a reference to the ConnectivityManager to check state of network connectivity
        ConnectivityManager connMgr = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);

        // Get details on the currently active default data network
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        // If there is a network connection, fetch data
        if (networkInfo != null && networkInfo.isConnected()) {
            // Get a reference to the LoaderManager, in order to interact with loaders.
            LoaderManager loaderManager = getLoaderManager();

            // Initialize the loader. Pass in the int ID constant defined above and pass in null for
            // the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
            // because this activity implements the LoaderCallbacks interface).
            loaderManager.initLoader(BOOKS_LOADER_ID, null, this);
        } else {
            // Otherwise, display error
            // First, hide loading indicator so error message will be visible
            View loadingIndicator = findViewById(R.id.loading_spinner);
            loadingIndicator.setVisibility(View.GONE);

            // Update empty state with no connection error message
            mEmptyStateTextView.setText(R.string.no_internet_connection);
            new BooksLoader(this, GOOGLEBOOKS_REQUEST_URL);
        }

    }

    public Loader<List<Books>> onCreateLoader(int i, Bundle bundle) {

        Log.i(LOG_TAG, "TEST: Book Activity Loader,list,books called");
        return new BooksLoader(this, GOOGLEBOOKS_REQUEST_URL);

    }

    public void onLoadFinished(Loader<List<Books>> loader, List<Books> book) {
        ProgressBar info = (ProgressBar) findViewById(R.id.loading_spinner);
        assert info != null;
        info.setVisibility(View.GONE);
        // Set empty state text to display "No Books found."
        mEmptyStateTextView.setText(R.string.no_books);

        Log.i(LOG_TAG, "TEST: Books Activity onLoadFinished() called");
        // Clear the adapter of previous book data
        mAdapter.clear();

        // If there is a valid list of {@link Books}s, then add them to the adapter's
        // data set. This will trigger the ListView to update.
        if (book != null && !book.isEmpty()) {
            mAdapter.addAll(book);

        }

    }

    @Override
    public void onLoaderReset(Loader<List<Books>> loader) {
        Log.i(LOG_TAG, "TEST: Books Activity onLoaderReset() called");
        // Loader reset, so we can clear out our existing data.
        mAdapter.clear();
    }

}
Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49

0 Answers0