0

通过网络调用加载数据,当设备旋转时,数据存储在 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");
    }

}
4

1 回答 1

1

在您log检索 中的值之前savedInstanceState,您将不会记录检索到的值。

看起来您可能必须向适配器类添加一个 addAll 方法(如果您还没有),然后以这种方式添加电影数据:

if(savedInstanceState != null){
    movies = savedInstanceState.getParcelableArrayList("list");
    madapter.addAll(movies); // Make sure you add an addAll() in your adapter's class first
    madapter.notifyDataSetChanged();
}
// The rest of your code

在您的适配器类中添加以下内容:

public void addAll(ArrayList al){
    for(int i=0; i < al.size; i++){
        // add to your adapter's data set
        // yourDataSet.add(al.get(i));
    }
}
于 2017-01-01T09:16:37.487 回答