如果我删除收藏夹中的电影,则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();
}
2条答案
按热度按时间n3ipq98p1#
我必须在Fragment类的onResume函数中更新我的回收视图。
选项FavoritesDB和Recycler此函数的观点是:
luaexgnf2#
我认为你应该只从dao那里得到你最喜欢的电影。
替换为:
有了这个:
在你的dao界面中,它看起来像这样。