Restar Loader无法更新gridview

时间:2015-09-12 14:49:14

标签: sql android-contentprovider loader

我有两个SQL表内连接与内容提供程序查询构建器。我的装载机在网格视图中显示第一个像魅力。已创建第二个表以显示收藏列表,因此它具有主键,外键和另一列。顺便说一下,当我尝试从这个中检索值时,我得到null。请一些建议!

我在内容提供商中有这个:

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

    }

2 个答案:

答案 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上运行该连接。我的猜测是查询有一些问题。

答案 1 :(得分: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
        );
    }