android-fragments 在片段中执行活动后更新recyclerview

ymzxtsji  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(204)

如果我删除收藏夹中的电影,则BackPressed上的收藏夹按钮。但当我对收藏夹中的电影进行分段时,回收器视图未更新。我希望在从movieDetailActivity返回时更新回收器视图

public class FavoritesFragment extends Fragment {

    private List<MoviesForFavorites> moviesForFavoritesList = new ArrayList<>();
    private FragmentFavoritesBinding favoritesBinding;
    private FavoriteMoviesAdapter favoriteMoviesAdapter;
    private String TAG = "Fragment_Favorites";
    private MoviesDao userDao;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        favoritesBinding = FragmentFavoritesBinding.inflate(inflater, container, false);

        AppDatabase db = Room.databaseBuilder(getActivity(), AppDatabase.class, "database-name").allowMainThreadQueries().build();
        userDao = db.moviesDao();
        System.out.println("geldiiiiii");
        moviesForFavoritesList.clear();
        moviesForFavoritesList = userDao.getAll();
        System.out.println(moviesForFavoritesList.size());
        System.out.println(moviesForFavoritesList.toString());

        favoritesBinding.recyclerViewFavorites.setHasFixedSize(true);
        favoritesBinding.recyclerViewFavorites.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        favoriteMoviesAdapter = null;
        favoriteMoviesAdapter = new FavoriteMoviesAdapter(getContext(),moviesForFavoritesList);
        favoriteMoviesAdapter.notifyDataSetChanged();
        favoritesBinding.recyclerViewFavorites.setAdapter(favoriteMoviesAdapter);

        return favoritesBinding.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();
        System.out.println("onStart fragment");

    }

    @Override
    public void onResume() {
        super.onResume();
        System.out.println("onResume fragment");
        System.out.println(moviesForFavoritesList.size());

    }

    @Override
    public void onStop() {
        super.onStop();
        System.out.println("onStop fragment");
    }
}

在活动中按下BackPressed

@Override
public void onBackPressed() {
    super.onBackPressed();
    System.out.println("onBackPressed");
    finish();

}

n3ipq98p

n3ipq98p1#

我必须在Fragment类的onResume函数中更新我的回收视图。

@Override
public void onResume() {
    super.onResume();
    optionsFavoritesDBandRecyclerview();  // update recyclerview list

}

选项FavoritesDB和Recycler此函数的观点是:

public void optionsFavoritesDBandRecyclerview(){

    AppDatabase db = Room.databaseBuilder(getActivity(), AppDatabase.class, "database-name").allowMainThreadQueries().build();
    MoviesDao userDao = db.moviesDao();

    moviesForFavoritesList.clear();
    moviesForFavoritesList = userDao.getAll();
    if(moviesForFavoritesList.size() == 0){
        favoritesBinding.imageViewFavoritesListEmpty.setVisibility(View.VISIBLE);
        favoritesBinding.textViewFavoritesListEmpty.setVisibility(View.VISIBLE);
    }
    else {
        favoritesBinding.imageViewFavoritesListEmpty.setVisibility(View.INVISIBLE);
        favoritesBinding.textViewFavoritesListEmpty.setVisibility(View.INVISIBLE);
    }
    favoritesBinding.recyclerViewFavorites.setHasFixedSize(true);
    favoritesBinding.recyclerViewFavorites.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
    favoriteMoviesAdapter = null;
    favoriteMoviesAdapter = new FavoriteMoviesAdapter(getContext(),moviesForFavoritesList);
    favoritesBinding.recyclerViewFavorites.setAdapter(favoriteMoviesAdapter);
    favoriteMoviesAdapter.notifyDataSetChanged();

}
luaexgnf

luaexgnf2#

我认为你应该只从dao那里得到你最喜欢的电影。
替换为:

userDao.getAll();

有了这个:

userDao.getFavorites();

在你的dao界面中,它看起来像这样。

@Query("SELECT * FROM Films WHERE isFavorite=1")
public List<Favorites> getFavorites();

相关问题