SwipeRefreshLayout is returning duplicate data when I refresh it, I have tried other solutions here but still not working. It's still duplicating my data when I refresh it.The data is retrieved correctly but the main issue is that it duplicates more and more whenever I refresh. I am using some sort of architecture for the app meaning that you might not be able to see where I did the api call and judging from the fact that the data has been retrieved from the server as requested so I don't think that there is any need for me to post those other code as the issue is only on duplicate data caused by swipe refresh layout. Here is my code
public class HomeActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ProgressDialog pd;
private SwipeRefreshLayout swipeRefreshLayout;
private List<MovieModel> moviesList;
private HomeMovieAdapter adapter;
private MovieListViewModel movieListViewModel;
private FavouriteDbHelper favouriteDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
recyclerView = findViewById(R.id.recyclerview_home);
swipeRefreshLayout = findViewById(R.id.main_content_home);
Toolbar toolbar = findViewById(R.id.toolBar_home);
favouriteDbHelper = new FavouriteDbHelper(this);
moviesList = new ArrayList<>();
movieListViewModel = new ViewModelProvider(this).get(MovieListViewModel.class);
setSupportActionBar(toolbar);
initViews();
}
private void initViews(){
pd = new ProgressDialog(this);
pd.setMessage("Fetching Movies...");
pd.setCancelable(false);
pd.show();
adapter = new HomeMovieAdapter(this,moviesList);
if (getApplicationContext().getResources().getConfiguration().orientation== Configuration.ORIENTATION_PORTRAIT){
recyclerView.setLayoutManager(new GridLayoutManager(this,2));
}else {
recyclerView.setLayoutManager(new GridLayoutManager(this,4));
}
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_orange_dark);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
initViews();
Toast.makeText(HomeActivity.this, "Movies Refreshed", Toast.LENGTH_SHORT).show();
}
});
observePopularMoviesRequest();
}
private void observePopularMoviesRequest(){
movieListViewModel.searchMoviesPage(1);
movieListViewModel.getPopularMovies().observe(this, new Observer<List<MovieModel>>() {
@Override
public void onChanged(List<MovieModel> movieModels) {
try {
//getting the retrieved data from the api
if (movieModels !=null){
for (MovieModel movieModel: movieModels){
Log.d("Main","The movies id are: "+movieModel.getMovie_id());
}
moviesList.addAll(movieModels);
recyclerView.setAdapter(new HomeMovieAdapter(getApplicationContext(),moviesList));
recyclerView.smoothScrollToPosition(0);
if (swipeRefreshLayout.isRefreshing()){
swipeRefreshLayout.setRefreshing(false);
}
pd.dismiss();
}
}catch (Exception e){
e.printStackTrace();
}
}
});
}
You need to call movieList.clear()
before you call refresh data so your data not duplicate
for example:
private void observePopularMoviesRequest(){
movieList.clear() //add this line
movieListViewModel.searchMoviesPage(1);
movieListViewModel.getPopularMovies().observe(this, new Observer<List<MovieModel>>() {
@Override
public void onChanged(List<MovieModel> movieModels) {
try {
//getting the retrieved data from the api
if (movieModels !=null){
for (MovieModel movieModel: movieModels){
Log.d("Main","The movies id are: "+movieModel.getMovie_id());
}
moviesList.addAll(movieModels);
recyclerView.setAdapter(new HomeMovieAdapter(getApplicationContext(),moviesList));
recyclerView.smoothScrollToPosition(0);
if (swipeRefreshLayout.isRefreshing()){
swipeRefreshLayout.setRefreshing(false);
}
pd.dismiss();
}
}catch (Exception e){
e.printStackTrace();
}
}
});