通过网络调用加载数据,当设备旋转时,数据存储在 onSaveInstanceState 回调中,适配器被通知更改,但旋转后活动变为空白。下面给出的代码片段:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
movies=new ArrayList<Movie_model>();
madapter=new Movie_adapter(this,new Movie_adapter.OnItemClickListener(){
@Override
public void onItemClick(Movie_model movie_item) {
Toast.makeText(getApplicationContext(), "Item Clicked", Toast.LENGTH_LONG).show();
Intent intent=new Intent(getApplicationContext(),MovieDetail.class);
intent.putExtra("Movie_item", movie_item);
intent.putExtra("type", "normal");
startActivity(intent);
};
},movies);
mAdapterFav = new CustomCursorAdapter(this,new CustomCursorAdapter.OnItemClickListener(){
@Override
public void onItemClick(Movie_model movie_item) {
Toast.makeText(getApplicationContext(), "Item Clicked", Toast.LENGTH_LONG).show();
Intent intent=new Intent(getApplicationContext(),MovieDetail.class);
intent.putExtra("Movie_item", movie_item);
intent.putExtra("type", "fav");
startActivity(intent);
};});
recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(),3);
recyclerview.setLayoutManager(mLayoutManager);
recyclerview.setItemAnimator(new DefaultItemAnimator());
recyclerview.setAdapter(madapter);
if(savedInstanceState!=null)
{
// movies.clear();
Log.i("tag", String.valueOf(movies.size()));
movies=savedInstanceState.getParcelableArrayList("list");
madapter.notifyDataSetChanged();
//recyclerview.setAdapter(madapter);
// Log.i("tag", String.valueOf(movies.size()));
}
else
{
mnetworking= new Networking();
mnetworking.execute(discover_movies);
Log.i("tag","in");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("list",movies);
Log.i("tag", String.valueOf(movies.size()));
}
另请注意 getParcelableArrayList("list"); 之后的列表大小为 20;叫做。令人难以置信的是,当适配器在 if 语句中再次初始化时,数据会在旋转时显示
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
movies=new ArrayList<Movie_model>();
madapter=new Movie_adapter(this,new Movie_adapter.OnItemClickListener(){
@Override
public void onItemClick(Movie_model movie_item) {
Toast.makeText(getApplicationContext(), "Item Clicked", Toast.LENGTH_LONG).show();
Intent intent=new Intent(getApplicationContext(),MovieDetail.class);
intent.putExtra("Movie_item", movie_item);
intent.putExtra("type", "normal");
startActivity(intent);
};
},movies);
mAdapterFav = new CustomCursorAdapter(this,new CustomCursorAdapter.OnItemClickListener(){
@Override
public void onItemClick(Movie_model movie_item) {
Toast.makeText(getApplicationContext(), "Item Clicked", Toast.LENGTH_LONG).show();
Intent intent=new Intent(getApplicationContext(),MovieDetail.class);
intent.putExtra("Movie_item", movie_item);
intent.putExtra("type", "fav");
startActivity(intent);
};});
recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(),3);
recyclerview.setLayoutManager(mLayoutManager);
recyclerview.setItemAnimator(new DefaultItemAnimator());
recyclerview.setAdapter(madapter);
if(savedInstanceState!=null)
{
// movies.clear();
Log.i("tag", String.valueOf(movies.size()));
movies=savedInstanceState.getParcelableArrayList("list");
// madapter.notifyDataSetChanged();
madapter=new Movie_adapter(this,new Movie_adapter.OnItemClickListener(){
@Override
public void onItemClick(Movie_model movie_item) {
Toast.makeText(getApplicationContext(), "Item Clicked", Toast.LENGTH_LONG).show();
Intent intent=new Intent(getApplicationContext(),MovieDetail.class);
intent.putExtra("Movie_item", movie_item);
intent.putExtra("type", "normal");
startActivity(intent);
};
},movies);
recyclerview.setAdapter(madapter);
Log.i("tag", String.valueOf(movies.size()));
}
else
{
mnetworking= new Networking();
mnetworking.execute(discover_movies);
Log.i("tag","in");
}
}