在我的下面的简单代码中,为什么我的适配器在更改旋转设备后无法刷新添加的新数据?
添加到第一次没有任何问题的适配器和刷新,但是在向其添加一些数据并更改设备方向之后。
我正在添加其他数据,我希望刷新添加新的,例如在第一时间我向适配器添加1个数据,适配器可以知道并且可以刷新,我看到添加的数据适配器,之后,我将设备方向从例如portrait
更改为landscape
现在我再次向适配器添加1个数据,我希望适配器可以刷新添加2个项目。
适配器有2项但无法刷新,有提示:
更改方向后可以使用上次添加的项目刷新设备适配器
public class MainActivity extends AppCompatActivity {
private List<Movie> movieList = new ArrayList<>();
private RecyclerView recyclerView;
private MoviesAdapter mAdapter;
private Movie movie;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new MoviesAdapter(movieList);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, int position) {
movie = new Movie("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014");
movieList.add(movie);
mAdapter.notifyDataSetChanged();
}
@Override
public void onLongClick(View view, int position) {
}
}));
if (savedInstanceState != null) {
movieList = savedInstanceState.getParcelableArrayList("key");
mAdapter.setData(movieList);
mAdapter.notifyDataSetChanged();
} else {
prepareMovieData();
}
}
private void prepareMovieData() {
movie = new Movie("Mad Max: Fury Road", "Action & Adventure", "2015");
movieList.add(movie);
mAdapter.notifyDataSetChanged();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList("key", (ArrayList) movieList);
super.onSaveInstanceState(outState);
}
}
MoviesAdapter:
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
private List<Movie> moviesList;
public void setData(List<Movie> mMovieList) {
moviesList.clear();
moviesList.addAll(mMovieList);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, year, genre;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
genre = (TextView) view.findViewById(R.id.genre);
year = (TextView) view.findViewById(R.id.year);
}
}
public MoviesAdapter(List<Movie> moviesList) {
this.moviesList = moviesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.movie_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Movie movie = moviesList.get(position);
holder.title.setText(movie.getTitle());
holder.genre.setText(movie.getGenre());
holder.year.setText(movie.getYear());
}
@Override
public int getItemCount() {
return moviesList.size();
}
}
电影:
public class Movie implements Parcelable {
private String title, genre, year;
public Movie() {
}
public Movie(String title, String genre, String year) {
this.title = title;
this.genre = genre;
this.year = year;
}
public String getTitle() {
return title;
}
public void setTitle(String name) {
this.title = name;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
@Override
public int describeContents() {
return 0;
}
private Movie(Parcel in) {
title = in.readString();
genre = in.readString();
year = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeString(genre);
dest.writeString(year);
}
public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>() {
public Movie createFromParcel(Parcel in) {
return new Movie(in);
}
public Movie[] newArray(int size) {
return new Movie[size];
}
};
}