0

I am developping an Android app which loads reddits and put it in a db, I use an asynchron cursor loader in my fragment SubredditsFragment.class. This fragment contains an adapter, which has a cursor loader. When I stop or reset the loader, the loader needs to be swapped on my adapter.

    public class SubRedditsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {


        private List<SubRedditData> subRedditDataList;

        private IntentFilter filter;

        public static final String TAG = SubRedditsFragment.class.getName();

        private SubredditAdapter adapter;
        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            String[] projection = null;
            String where = null;
            String[] whereArgs = null;
            String sortOrder = null;

            Uri queryUri = RedditContentProvider.CONTENT_URI;

            return new CursorLoader(getActivity(), queryUri, projection, where, whereArgs, sortOrder);
        }

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG,"Added broadcastreceiver");
        getActivity().registerReceiver(receiver,filter);
        adapter = new SubredditAdapter(getActivity().getApplicationContext(),subRedditDataList);    

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        adapter.swapCursor(data);

            getLoaderManager().destroyLoader(loader.getId());
        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
            adapter.swapCursor(null);
        }

The problem is that I can't use the method adapter.swapCursor(), it's unknown for Android. I get the error message Cannot resolve method 'swapCursor(loader)'
Jonas
  • 769
  • 1
  • 8
  • 37

1 Answers1

0

add this code in your Adapter, mCursor is the global cursor variable

public void swapCursor(Cursor newCursor) {
        // Always close the previous mCursor first
        if (mCursor != null) mCursor.close();
        mCursor = newCursor;
        if (newCursor != null) {
            // Force the RecyclerView to refresh
            this.notifyDataSetChanged();
        }
    }
Kundan
  • 590
  • 9
  • 21
  • The important thing here is that recyclerview is used which does very little by itself. You have to add to the adapter. – Theo Nov 09 '17 at 14:45