我在Stackoverflow中读到了很多关于这个问题的帖子......我实现了我见过的所有解决方案,但都没有。
发生了什么:打开应用程序时,notifyDataSetChanged不起作用。如果我改变方向或更改排序(流行到最高评级,或反之亦然),它的工作原理。我重新编写了代码并且数据在适配器中正确到达,但是通知不起作用,因此接口没有更新。
有人可以帮助我吗?
我的onCreate方法:
@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);
}
loadMoviesData方法只是从AsyncTask调用execute()。
我的onPostExecute代码:
@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();
}
}
我的setMovieList方法:
public void setMovieList(Movies movieList) {
this.movieList = movieList;
this.notifyDataSetChanged();
}