Home > Blockchain >  Update recyclerview after activity in fragment
Update recyclerview after activity in fragment

Time:07-26

Favorites button onBackPressed and if i delete film in favorites. but when i go to fragment favorites films, recycler view didnt update. I want to update recyclerview when i come from 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");
    }
}

in activity onBackPressed

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

}

enter image description here

enter image description here

CodePudding user response:

I have to do that update my recyclerview in onResume function in Fragment class.

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

}

optionsFavoritesDBandRecyclerview this function is that:

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

}

CodePudding user response:

I think you should only get your favorite movies from dao.

Replace this:

userDao.getAll();

With this:

userDao.getFavorites();

In your dao interface it will look something like this

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