(对不起,我不知道我的英语是否正确,我希望它是正确的!)
loadInBackground()方法(在BookLoader类中)返回一个字符串值,但是谁呢?
我也在寻找谁调用loadInBackground(),但没有人这样做。我也阅读了官方文档,但没有解决方案。

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

public EditText mEditText;
public TextView mTextTitle, mTextAuthor;

@Override
protected void onCreate(Bundle savedInstanceState) {

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

    mEditText = (EditText) findViewById(R.id.bookInput);
    mTextTitle = (TextView) findViewById(R.id.titleText);
    mTextAuthor = (TextView) findViewById(R.id.authorText);

    // to reconnect to the Loader if it already exists
    if(getSupportLoaderManager().getLoader(0)!=null){

        getSupportLoaderManager().initLoader(0,null,this);

    }

}

public void searchBooks(View view) {

    String queryString = mEditText.getText().toString();

    // nasconde la tastiera
    InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

    // Check the status of the network connection.
    ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected() && queryString.length()!=0) {

        mTextAuthor.setText("");
        mTextTitle.setText(R.string.loading);

        Bundle queryBundle = new Bundle();
        queryBundle.putString("queryString", queryString);

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

    }

    else {

        if (queryString.length() == 0) {

            mTextAuthor.setText("");
            mTextTitle.setText("Please enter a search term");

        } else {

            mTextAuthor.setText("");
            mTextTitle.setText("Please check your network connection and try again.");

        }

    }

}

// Called when you instantiate your Loader.
@NonNull
@Override
public Loader<String> onCreateLoader(int i, @Nullable Bundle bundle) {

    return new BookLoader(this, bundle.getString("queryString"));

}


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

    try {

        JSONObject jsonObject = new JSONObject(s);
        JSONArray itemsArray = jsonObject.getJSONArray("items");

        int i = 0;
        String title = null;
        String authors = null;

        while (i < itemsArray.length() || (authors == null && title == null)) {
            // Get the current item information.
            JSONObject book = itemsArray.getJSONObject(i);
            JSONObject volumeInfo = book.getJSONObject("volumeInfo");

            // Try to get the author and title from the current item,
            // catch if either field is empty and move on.
            try {

                title = volumeInfo.getString("title");
                authors = volumeInfo.getString("authors");
                Log.d("TITLE", volumeInfo.getString("title"));

            } catch (Exception e){

                e.printStackTrace();

            }

            // Move to the next item.
            i++;
        }

        // If both are found, display the result.
        if (title != null && authors != null){

            mTextTitle.setText(title);
            mTextAuthor.setText(authors);
            mEditText.setText("");

        } else {
            // If none are found, update the UI to show failed results.
            mTextTitle.setText("no results");
            mTextAuthor.setText("");

        }

    } catch (Exception e){
        // If onPostExecute does not receive a proper JSON string, update the UI to show failed results.
        mTextTitle.setText("no results");
        mTextAuthor.setText("");
        e.printStackTrace();
    }

}

// Cleans up any remaining resources.
@Override
public void onLoaderReset(@NonNull Loader<String> loader) {

}

}




public class BookLoader extends AsyncTaskLoader<String> {

String mQueryString;

public BookLoader(@NonNull Context context, String queryString) {

    super(context);
    mQueryString = queryString;

}

@Nullable
@Override
public String loadInBackground() {

    return NetworkUtils.getBookInfo(mQueryString);

}

@Override
protected void onStartLoading() {

    super.onStartLoading();

    forceLoad();

}


}

我编辑了帖子。

最佳答案

如官方文件所述:


  onPostExecute(Result),在后台计算完成后在UI线程上调用。后台计算的结果作为参数传递到此步骤。


您的返回值将发送到onPostExecute()。

我鼓励您深入了解this documentation

08-26 05:03