Guilherme Lima Pereira
Guilherme Lima Pereira

Reputation: 1434

notifyDataSetChanged not working when open the App

I've read so many posts here in Stackoverflow about this problem... I implemented all the solution that I've seen, but none of them worked.

What is happening: the notifyDataSetChanged doesn't work when opening the app. If I change orientation or change the sort (popular to top rated, or vise-versa), it works. I debbuged the code and the data arrives correctly in the Adapter, but the notify doesn't work, so the interface doesn't got updated.

Can anyone help me, please?

My onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Resources rs = getResources();
    int numColumns = rs.getInteger(R.integer.list_columns);
    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_popular_movies);
    mErrorMessageDisplay = (TextView)findViewById(R.id.tv_error_message_display);
    mLoadingIndicator = (ProgressBar) findViewById(R.id.pb_loading_indicator);

    //Resource based on https://discussions.udacity.com/t/is-there-a-way-to-fit-columns-in-gridlayoutmanager/221936/4
    GridLayoutManager layoutManager = new GridLayoutManager(this, numColumns);
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setHasFixedSize(true);

    mPopularMoviesAdapter = new PopularMoviesAdapter(this, this);
    mRecyclerView.setAdapter(mPopularMoviesAdapter);

    loadMoviesData(NetworkUtils.POPULAR_SORT);
}

The loadMoviesData method simply calls the execute() from AsyncTask.

My onPostExecute code:

@Override
protected void onPostExecute(String result) {
    Log.v(TAG, "onPostExecute");
    mLoadingIndicator.setVisibility(View.INVISIBLE);
    if(result != null){
        showMoviesDataView();
        Gson gson = new Gson();
        Movies data = gson.fromJson(result, Movies.class);
        mPopularMoviesAdapter.setMovieList(data);
    } else {
        showErrorMessage();
    }
}

My setMovieList method:

public void setMovieList(Movies movieList) {
    this.movieList = movieList;
    this.notifyDataSetChanged();
}

My full code: https://github.com/guuilp/PopularMovies

Upvotes: 1

Views: 191

Answers (1)

Luís Ramalho
Luís Ramalho

Reputation: 10208

I had a look at your code and your notifyDataSetChanged() is working, the problem is that your RecyclerView has no height.

Change it to android:layout_height="match_parent".

enter image description here

Upvotes: 4

Related Questions