0

從我的ContentProvider獲取的數據未加載到屏幕中。我不知道問題在哪裏,尤其是在onLoadFinished中傳遞的數據變量不爲空,並且ContentProvider的查詢方法也不會返回null。這裏是Loader回調的實現。數據未加載到片段

的data.getCount()在onLoadFinished返回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); 
    } 

查詢不返回null,在onCreateLoader開始沒有返回null裝載機,無論是在onLoadFinished返回空接收到的數據。

任何想法可能導致數據不被加載到Fragment的問題是什麼?

+0

首先在initLoader方法之前放置super.onActivityCreated。你在cursorLoader中放置了一些斷點,結果可能是什麼?你是否對數據庫檢查了它實際返回一些數據的查詢? – Aegis

回答

0

我有同樣的問題時,該適配器屬於一個ViewPager

此代碼解決我的問題

@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); 
    } 

使用適配器屬於一個GridView只

   mAdapter.notifyDataSetChanged(); 

是必要的。