2015-09-12 74 views
0

我有兩個SQL表內連接與內容提供商查詢生成器。我的加載程序在gridview中顯示第一個像魅力。第二個表格已經被創建來顯示最愛列表,所以它有主鍵,外鍵和另一列。順便說一下,當我嘗試從這一個檢索值我得到空。請提出一些建議!Restar Loader無法更新gridview

我有這樣的內容提供商:

static { 
     sMovieByFavoriteQueryBuilder = new SQLiteQueryBuilder(); 

     sMovieByFavoriteQueryBuilder.setTables(
       MoviesContract.FavoriteEntry.TABLE_NAME + " INNER JOIN " + 
         MoviesContract.MovieEntry.TABLE_NAME + 
         " ON " + MoviesContract.FavoriteEntry.TABLE_NAME + 
         "." + MoviesContract.FavoriteEntry.COLUMN_FAVORITE_KEY + 
         " = " + MoviesContract.MovieEntry.TABLE_NAME + 
         "." + MoviesContract.MovieEntry._ID); 
    } 

    private static final String sFavoriteSelection = 
      MoviesContract.FavoriteEntry.TABLE_NAME + 
        "." + MoviesContract.FavoriteEntry.COLUMN_MOVIE_ID + " = ? AND " + 
        MoviesContract.FavoriteEntry.COLUMN_MOVIE_POSTER + " = ? AND " + 
        MoviesContract.MovieEntry.COLUMN_RELEASE_DATE + " = ? AND " + 
        MoviesContract.MovieEntry.COLUMN_MOVIE_POSTER + " = ? AND " + 
        MoviesContract.MovieEntry.COLUMN_ORIGINAL_TITLE + " = ? AND " + 
        MoviesContract.MovieEntry.COLUMN_SYNOSIS + " = ? AND " + 
        MoviesContract.MovieEntry.COLUMN_USER_RATING + " = ? "; 

I call this method by uri from fragment: 
@Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     getLoaderManager().initLoader(MOVIES_LOADER, null, this); 
     getLoaderManager().initLoader(FAVORITE_LOADER, null, this); 
     super.onActivityCreated(savedInstanceState); 
    } 

    void onSortChanged() { 
     System.out.println("onSortChanged: true"); 
     updateMovies(); 
     getLoaderManager().restartLoader(MOVIES_LOADER, null, this); 
     System.out.println("LoaderManager: " + getLoaderManager().restartLoader(MOVIES_LOADER, null, this)); 
    } 

    void onFavorite() { 

     getLoaderManager().restartLoader(FAVORITE_LOADER, null, this); 
     mMoviesAdapter.setSelectedIndex(mPosition); 
     mMoviesAdapter.notifyDataSetChanged(); 
    } 


    private void updateMovies() { 
     PopularMoviesSyncAdapter.syncImmediately(getActivity()); 
    } 

    @Override 
    public Loader<Cursor> onCreateLoader(int id, Bundle args) { 

     if (MainActivity.mFavorite == true) { 
      String sortOrder = MoviesContract.FavoriteEntry.COLUMN_FAVORITE_KEY; 
      String sortSetting = Utility.getPreferredSort(getActivity()); 
      Uri movieFavoriteUri = MoviesContract.MovieEntry.buildMovieWithSortDate(sortSetting, System.currentTimeMillis()); 
      System.out.println("movieFavoriteUri: " + movieFavoriteUri); 
      cursorFav = new CursorLoader(getActivity(), 
        movieFavoriteUri, 
        FAVORITE_COLUMNS, 
        null, 
        null, 
        sortOrder); 
      return cursorFav; 

     } else { 
      String sortOrder = MoviesContract.MovieEntry.COLUMN_DATE; 
      String sortSetting = Utility.getPreferredSort(getActivity()); 
      System.out.println("sortSetting: " + sortSetting); 
      Uri movieForSortUri = MoviesContract.MovieEntry.buildMovieSort(sortSetting); 

      System.out.println("movieForSortUri: " + movieForSortUri); 
      cursorMov = new CursorLoader(getActivity(), 
        movieForSortUri, 
        MOVIES_COLUMNS, 
        null, 
        null, 
        sortOrder); 
      return cursorMov; 

     } 
    } 

    @Override 
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 
     mMoviesAdapter.swapCursor(data); 
     if (mPosition != GridView.INVALID_POSITION) { 
      // If we don't need to restart the loader, and there's a desired position to restore 
      // to, do so now. 
      mGridView.smoothScrollToPosition(mPosition); 
     } 
    } 

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

    } 
+0

所以,如果你查詢每個單獨的表wihout加入你得到的數據?如果是這樣的話,問題在於你如何加入。你能告訴我們其他的東西嗎? –

+0

是的,我不能只加入 –

回答

0

相反的:

sMovieByFavoriteQueryBuilder.setTables(
      MoviesContract.FavoriteEntry.TABLE_NAME + " INNER JOIN " + 
        MoviesContract.MovieEntry.TABLE_NAME + 
        " ON " + MoviesContract.FavoriteEntry.TABLE_NAME + 
        "." + MoviesContract.FavoriteEntry.COLUMN_FAVORITE_KEY + 
        " = " + MoviesContract.MovieEntry.TABLE_NAME + 
        "." + MoviesContract.MovieEntry._ID); 

創建一個字符串,這樣你就可以調試方便。

strTables = MoviesContract.FavoriteEntry.TABLE_NAME + " INNER JOIN " + 
        MoviesContract.MovieEntry.TABLE_NAME + 
        " ON " + MoviesContract.FavoriteEntry.TABLE_NAME + 
        "." + MoviesContract.FavoriteEntry.COLUMN_FAVORITE_KEY + 
        " = " + MoviesContract.MovieEntry.TABLE_NAME + 
        "." + MoviesContract.MovieEntry._ID); 

sMovieByFavoriteQueryBuilder.setTables(strTables); 

然後嘗試在db上直接運行該連接。我的猜測是查詢有一些問題。

+0

你是對的卡洛斯。我的查詢出了問題。解決使用rawquery而不是SQLiteQueryBuilder –

0

我檢查和查詢工程很好。但我的方法不能返回任何東西

private Cursor getMovieByFavorite(Uri uri, String[] projection, String movieId) { 
     String sortSetting = MoviesContract.MovieEntry.getFavoriteFromUri(uri); 
     long date = MoviesContract.MovieEntry.getDateFromUri(uri); 
     String[] selectionArgs; 
     String selection; 

     selection = sFavoriteSelection; 
     selectionArgs = new String[]{sortSetting}; 

     return sMovieByFavoriteQueryBuilder.query(mOpenHelper.getReadableDatabase(), 
       projection, 
       selection, 
       null, 
       null, 
       null, 
       movieId 
     ); 
    } 
+0

Thhis可能應該作爲另一種querstion。但是,什麼返回sMovieByFavoriteQueryBuilder.query()'? –