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 に答える