多次调用onStartLoading

时间:2017-02-22 17:54:33

标签: android loader

我遇到了Android Loaders的问题 我有一个从互联网数据填充的活动,我有一个书签选项,可以在本地存储和加载它们 我正在实施显示项目的recyclerView 当我更改排序条件时,adapter将被清除并重新填充新数据,当我选择查看已添加书签的项目时,本地查询将启动到ContentProvider。 现在,我对书签数据存在问题,因为我在adapter中获得了同一项目的多个副本。
我做了一些日志记录,我注意到在本地加载时多次调用加载程序(每次添加相同的项目),但我不明白为什么。
请注意,当我返回活动时也会发生这种情况,但是当我使用书签首选项启动应用程序时却不会发生这种情况 如果我从书签开始,选择一个书签并返回,也会进行多次调用 谁能帮我?这是代码:
MainActivity中,此方法在onCreate

结束时调用
 private void loadPosters() {
    Log.d(TAG,"Loading posters");
    if (mPagesLoaded < MAX_PAGES) {
        Bundle args = new Bundle();
        args.putInt("page",mPagesLoaded+1);
        getSupportLoaderManager().restartLoader(LOADER_ID,args,this);
    }
}

我的加载程序代码:

public Loader<ArrayList<Movie>> onCreateLoader(int id, final Bundle args) {
    return new AsyncTaskLoader<ArrayList<Movie>>(this) {
        ArrayList<Movie> mData;

        @Override
        protected void onStartLoading() {
            Log.d(TAG,"Start Loading");
            super.onStartLoading();
            if (mData!=null){
                deliverResult(mData);
            }else{
                if (mPagesLoaded == 0) {
                    mProgressBar.setVisibility(View.VISIBLE);
                }
                mErrorTextView.setVisibility(View.INVISIBLE);
                forceLoad();
            }
        }

        @Override
        public ArrayList<Movie> loadInBackground() {
            Log.d(TAG,"Load in background");
            if (args.size() == 0) {
                return null;
            }
            int page = args.getInt("page");
            NetworkUtils networker = new NetworkUtils(getApplicationContext());
            String criterion = getSharedPreferences(getString(R.string.movie_preferences), Context.MODE_PRIVATE).getString("sorting", "popular");
            if (!(criterion.equals(getString(R.string.pref_bookmarked)))) {
                URL request = networker.buildMoviesUrl(page, criterion);
                try {
                    String JSONResponse = networker.getResponseFromHttpUrl(request);
                    ArrayList<Movie> res =  fetchMoviesFromJson(JSONResponse);
                    mPagesLoaded++;
                    return res;

                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }
            else{
                Log.d(TAG,"Local Loading");
                Cursor cursor = getContentResolver().query(MovieContract.MovieEntry.CONTENT_URI,null,null,null,null);
                if (cursor!=null){
                    Log.d(TAG,"Cursor is not null");
                    ArrayList<Movie> res = fetchMoviesFromCursor(cursor);
                    cursor.close();
                    return res;
                }
                return null;
            }

        }

        @Override
        public void deliverResult(ArrayList<Movie> data) {
            mData = data;
            mProgressBar.setVisibility(View.INVISIBLE);
            super.deliverResult(data);
        }
    };
}

我的onLoadFinished回调:

@Override
public void onLoadFinished(Loader<ArrayList<Movie>> loader, ArrayList<Movie> movies) {
    Log.d(TAG,"Load finished");
    mProgressBar.setVisibility(View.INVISIBLE);
    if (movies != null) {
        mPostersAdapter.addMovies(movies);
        Log.d(TAG,mPostersAdapter.getItemCount() + " items loaded");
        showPosters();
    } else {
        showErrorMessage();
    }
}

我的SharedPreferences代码:

private void initSharedPreferences() {
    mSharedPrefs = getApplicationContext().getSharedPreferences("movie_preferences", MODE_PRIVATE);
    mOnSharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            Log.d(TAG, "Shared preferences for " + key + "changed. Pref: " + sharedPreferences.getString(key, null));
            mPagesLoaded = 0;
            mPostersAdapter.clear();
            loadPosters();
        }
    };
    mSharedPrefs.registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);

}

1 个答案:

答案 0 :(得分:1)

我必须在装载程序管理器上调用destoryLoader()来解决此问题。不确定这是否正确..