loadInBackground()的返回值传递给谁?

问题描述 投票:0回答:1

(对不起,我不知道我的英文是否正确,我希望它是对的!)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();

}

}

我编辑了这篇文章。

java android loader
1个回答
1
投票

正如官方文件所说:

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

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

我鼓励你深入了解this documentation

© www.soinside.com 2019 - 2024. All rights reserved.