RandomyzeEverything
RandomyzeEverything

Reputation: 784

notifyDatasetChanged() not working after sorting RecyclerView

My RecyclerView contains check marks in front of movies which are seen. I am using notifyDatasetChanged() after sorting my RecyclerView. After clicking the menu options, even though items are changing, the check marks are not changing and stay in the same place.

Here's a part of my activity class:

public class SearchActivity extends AppCompatActivity {
    MoviesAdapter adapter;
    List<Movie> movies = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        adapter = new MovieAdapter(movies, SearchActivity.this);
        recyclerView.setAdapter(adapter);

        fetchMovies("ascending");
    }

    private void fetchMovies(String order) {
        movies.clear();
        movies.addAll(response.body().getMovies(order));
        adapter.notifyDatasetChanged();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.ascending:
                fetchMovies("ascending");
                adapter.notifyDatasetChanged();
                return true;
            case R.id.descending:
                fetchMovies("descending");
                adapter.notifyDatasetChanged();                    
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

My MovieAdapter:

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieViewHolder> {

private List<Movie> movies;
private final int rowLayout;
private final Context context;

public MovieAdapter(List<Movie> movies, Context context) {
    this.movies= movies;
    this.rowLayout = R.layout.movie_item;
    this.context = context;
}

@Override
public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
    return  new MovieViewHolder(view);
}

@Override
public void onBindViewHolder(final MovieViewHolder holder, @SuppressLint("RecyclerView") final int position) {...}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}

@Override
public int getItemCount() {
    return movies.size();
}

public void notifyNewData(List<Movie> movies) {
    this.movies= movies;
    notifyDataSetChanged();
}

static class MovieViewHolder extends RecyclerView.ViewHolder {...}

}

Upvotes: 3

Views: 1486

Answers (1)

Pro Mode
Pro Mode

Reputation: 1463

U should use movies.addAll() instead of movies.add(). Because its obvious you are adding more that one item from api response.

If it doesn't work, Create a method in your adapter class and pass the new values to it and call notifyDatasetChanged from within that method:

 public void notifyNewData(List<Movie> movies){
        this.movies.clear()
        this.addAll(movies)
        this.notifyDatasetChanged();
    }

and call adapter.notifyNewData(movies); from your main class

Upvotes: 1

Related Questions