如何将Cursor转换为CursorLoader?

时间:2018-03-22 12:59:39

标签: java android

我对SQLite和ContentProviders有一点问题。首先,这是我目前的代码:

public void loadFavoriteMovies(){
    movieList.clear();
    Cursor cursor = getContentResolver().query(FavoriteMoviesContract.MovieEntry.CONTENT_URI,
            null, null, null, FavoriteMoviesContract.MovieEntry._ID);


    if(cursor != null){
        while(cursor.moveToNext()) {

            MovieResults.Movie movie = new MovieResults.Movie(
                    cursor.getString(cursor.getColumnIndex(FavoriteMoviesContract.MovieEntry.COLUMN_TITLE)),
                    cursor.getString(cursor.getColumnIndex(FavoriteMoviesContract.MovieEntry.COLUMN_IMAGE)),
                    cursor.getString(cursor.getColumnIndex(FavoriteMoviesContract.MovieEntry.COLUMN_BACKDROP)),
                    cursor.getString(cursor.getColumnIndex(FavoriteMoviesContract.MovieEntry.COLUMN_RELEASE)),
                    Double.valueOf(cursor.getString(cursor.getColumnIndex(FavoriteMoviesContract.MovieEntry.COLUMN_RATING))),
                    cursor.getString(cursor.getColumnIndex(FavoriteMoviesContract.MovieEntry.COLUMN_PLOT)),
                    Integer.valueOf(cursor.getString(cursor.getColumnIndex(FavoriteMoviesContract.MovieEntry.COLUMN_ID)))
                    );

            movieList.add(movie);
        }
        cursor.close();
    }


    Log.v("Size of Favorites:", String.valueOf(movieList.size()));
    mAdapter.setmMovieList(movieList);
}

我正在尝试做的是我正在阅读SQLite数据库并将值放在Movie对象中。但我的问题是我想使用CursorLoader执行此操作。我怎样才能做到这一点?

我这样做的主要目的是因为当我从数据库中删除一个项目时,Cursor不会自动更新该值,我需要重新加载项目显示的活动。如果有人可以提供替代方式,那也值得赞赏。

0 个答案:

没有答案