如何在Loader类中将变量传递给loadInBackground()?

时间:2017-07-21 10:54:27

标签: java android android-asynctask loader

public class BookLoader extends AsyncTaskLoader<List<Book>> {

    public BookLoader(Context context, String baseRequestUrl) {
        super(context);
    }

    @Override
    protected void onStartLoading() {
        super.onStartLoading();
        forceLoad();
    }

    @Override
    public List<Book> loadInBackground() {
        /** Tag for log messages */
         String LOG_TAG = BookLoader.class.getName();
        final String APP_ID = "booklisting-174212";
        Log.e( " REQUEST is: trial 2 : "+baseRequestUrl );
        BASE_REQUEST_URL += key;
        Log.e(LOG_TAG, " Request url : "+BASE_REQUEST_URL );
        return null;
    }



}

我希望能够在loadInBackground函数中使用baseRequestUrl,我该如何实现?

1 个答案:

答案 0 :(得分:1)

试试这个:

 public class BookLoader extends AsyncTaskLoader<List<Book>> {
        String baseRequestUrl;
        public BookLoader(Context context, String baseRequestUrl) {
            super(context);
            this.baseRequestUrl=baseRequestUrl;
        }

        @Override
        protected void onStartLoading() {
            super.onStartLoading();
            forceLoad();
        }

        @Override
        public List<Book> loadInBackground() {
             //user url here
            /** Tag for log messages */
             String LOG_TAG = BookLoader.class.getName();
            final String APP_ID = "booklisting-174212";
            Log.e( " REQUEST is: trial 2 : "+baseRequestUrl );
            BASE_REQUEST_URL += key;
            Log.e(LOG_TAG, " Request url : "+BASE_REQUEST_URL );
            return null;
        }
}