使用'这个'在getSupportLoaderManager()中给我一个错误消息

时间:2017-10-25 18:30:50

标签: java android

我正在松散地关注指南,但我正在使用onClickListener来启动搜索过程。我正在这里实施一个Loader:

getSupportLoaderManager()restartLoader(0,queryBundle,此);

但当我输入'this'时会显示以下信息:

错误的第三个参数类型。找到:' android.view.View.OnClickListener',required:' android.support.v4.app.LoaderManager。 LoaderCallbacks'

我厌恶从onClickListener更改为onClick方法,如指南所示,有没有办法在onClickListener中实现Loader?

代码如下:

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) {

    }
}

1 个答案:

答案 0 :(得分:3)

this指的是封装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
    }
});

为了引用封装GoogleBookActivity,只需在前面输入该类名:

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