Search code examples
androidandroid-contentproviderandroid-adapterandroid-loadermanager

Data is not loaded to the Fragment


The data fetched from my ContentProvider is not loaded in the screen. I don't know where is the problem especially that data variable passed in onLoadFinished is not null and the query method from the ContentProvider doesn't return null too. Here is the implementation of the Loader callbacks.

The data.getCount() in onLoadFinished returns 0.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    getLoaderManager().initLoader(MOVIE_LOADER, null, this);
    super.onActivityCreated(savedInstanceState);
}

@Override
public android.support.v4.content.Loader<Cursor> onCreateLoader(int id, Bundle args) {
    android.support.v4.content.Loader cursorLoader;
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    pref = preferences.getString("sort_method", "0");
    String a;
    if (pref.equals("0")) {
        cursorLoader = new android.support.v4.content.CursorLoader(getActivity(), MovieContract.MostPopMovieEntry.CONTENT_URI,
                new String[]{MovieContract.MostPopMovieEntry._ID, MovieContract.MostPopMovieEntry.COLUMN_POSTER_PATH},
                null,
                null,
                null);
    } else {
        cursorLoader = new android.support.v4.content.CursorLoader(getActivity(), MovieContract.TopRatedMovieEntry.CONTENT_URI,
                new String[]{MovieContract.TopRatedMovieEntry._ID, MovieContract.TopRatedMovieEntry.COLUMN_POSTER_PATH},
                null,
                null,
                null);
    }
    if (cursorLoader != null) {
        a = "cursorLoader initiated in onCreateLoader is not null";
    } else {
        a = "cursorLoader initiated in onCreateLoader is null";
    }
    Log.d(LOG_TAG, a);

    return cursorLoader;
}

@Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        Log.d(LOG_TAG, data.getCount() + " rows loaded");
        if (data != null) {
            Log.d(LOG_TAG, "Data received onLoadfinished is no null");
        }else{
            Log.d(LOG_TAG, "Data received onLoadfinished is null");
        }
        movieAdapter.swapCursor(data);
    }

The query doesn't return null, the loader initiated in onCreateLoader doesn't return null, neither the data received in onLoadFinished returns null.

Any idea what might be the problem that causes the data not be loaded to the Fragment?


Solution

  • I had the same problem when the adapter belonged to a ViewPager

    This code solved my problem

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            // to be restored after reload
            mInitialScrollPosition = mViewPager.getCurrentItem();
    
            // do change the data
            mAdapter.swapCursor(data);
    
            // restore position is invalid
            if (mInitialScrollPosition >= mAdapter.getCount()) mInitialScrollPosition = -1;
    
            // do change the data
            mAdapter.notifyDataSetChanged();
            mViewPager.setAdapter(mAdapter);
    
            if (mInitialScrollPosition >= 0) mViewPager.setCurrentItem(mInitialScrollPosition);
        }
    

    With an adapter belonging to a GridView only

                  mAdapter.notifyDataSetChanged();
    

    was necessary.