简体   繁体   中英

use of 'this' in getSupportLoaderManager() gives me an error message

I am loosely following a guide but I am using an onClickListener to start the search process. I am in the process of implementing a Loader here:

getSupportLoaderManager().restartLoader(0,queryBundle,this);

but when I put in 'this' it brings up the following message:

Wrong 3rd argument type. Found: 'android.view.View.OnClickListener', required: 'android.support.v4.app.LoaderManager. LoaderCallbacks'

I am loathed to change from an onClickListener to a onClick method as the guide suggests, is there a way to implement the Loader in the onClickListener?

The code is below:

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GoogleBookActivity extends AppCompatActivity implements 
LoaderManager.LoaderCallbacks<String>{

private static final String TAG = GoogleBookActivity.class.getSimpleName();
private EditText mEditText;
private BookAdapter mAdapter;
private Button bookSearch;
private TextView mTitle, mAuthor;

//URL link to the API
private static final String GOOGLE_BOOKS_REQUEST_URL = "https://www.googleapis.com/books/v1/volumes?q=";

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

    mTitle = (TextView) findViewById(R.id.book_title);
    mAuthor = (TextView) findViewById(R.id.book_author);
    bookSearch = (Button) findViewById(R.id.searchbutton);
    mEditText = (EditText) findViewById(R.id.search_text);




   bookSearch.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {

           //Initialise String queryText
           String queryText = mEditText.getText().toString();

           //For Hiding the keyboard when the search button is clicked
           InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
           inputManager.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);

           //For Checking the network status and empty search field case
           ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
           NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

           if(networkInfo !=null && networkInfo.isConnected() && queryText.length()!=0){
               //new FetchBook(mTitle, mAuthor).execute(queryText);
           }

           Log.i(TAG, "Searched " + queryText);
           if(queryText.length() != 0){
               new FetchBook(mTitle, mAuthor).execute(queryText);
               mAuthor.setText("");
               mTitle.setText(R.string.loading);
               //We replace the call to execute the fetchbook task with a call to restartLoader(), passing in
               // the querystring you get from the EditText in the Bundle.
               Bundle queryBundle = new Bundle();
               queryBundle.putString("queryText", queryText);
               getSupportLoaderManager().restartLoader(0,queryBundle,this);

           }else {
               if(queryText.length() == 0){
                   mAuthor.setText("");
                   mTitle.setText("Please enter a search term.");
               }else{
                   mAuthor.setText("");
                   mTitle.setText("Please check your network connection and try again.");
               }
           }
       }
   });


}


@Override
public Loader<String> onCreateLoader(int i, Bundle bundle) {
    return null;
}

@Override
public void onLoadFinished(Loader<String> loader, String s) {

}

@Override
public void onLoaderReset(Loader<String> loader) {

    }
}

this refers to the encapsulating OnClickListener :

bookSearch.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        // You are now inside an anonymous class extending from
        // OnClickListener. 'this' now refers to this very instance
        // of the OnClickListener
    }
});

In order to refer to the encapsulating GoogleBookActivity , just type that class name in front:

getSupportLoaderManager().restartLoader(0, queryBundle, GoogleBookActivity.this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM