开发者

Rotating screen with more than one listview - list loses content

I have a strange problem when rotating the screen with multiple listviews.

I utilise:

 public void onConfigurationChanged(Configuration newConfig) {
 super.onConfigurationChanged(newConfig);

to maintain the pages contents when it is rotated. The initial listview maintains its content fine, but my other listview empties itself whenever the screen is rotated. This happens in 2 activities I have which do a similar thing.

The code for populating the views is below.

Does anybody know how I can maintain the listview data for both listviews?

Thanks

private void populateList() {
   // Get all of the fixtures from the database and create the item list
   Cursor c = mDbHelper.fetchAllFixtures();
   startManagingCursor(c);

   String[] from = new String[] { FixturesDbAdapter.KEY_JUSTDATE,      FixturesDbAdapter.KEY_J开发者_JAVA技巧USTTIME, FixturesDbAdapter.KEY_HOMETEAM, FixturesDbAdapter.KEY_HOMESCORE,FixturesDbAdapter.KEY_AWAYSCORE, FixturesDbAdapter.KEY_AWAYTEAM, FixturesDbAdapter.KEY_LOCATION, FixturesDbAdapter.KEY_COMPETITION};
   int[] to = new int[] { R.id.fixtextlist, R.id.fixtextlistkotime, R.id.fixtextlisthome, R.id.fixtextlisths, R.id.fixtextlistas, R.id.fixtextlistaway, R.id.fixtextliststad, R.id.fixtextlistcomp};

   // Now create an array adapter and set it to display using our row
   SimpleCursorAdapter fixtures =
       new SimpleCursorAdapter(this, R.layout.fixlist_item, c, from, to);
   setListAdapter(fixtures);

   Cursor c2 = mDbHelper.fetchAllResults();
   startManagingCursor(c2);

   String[] from2 = new String[] { FixturesDbAdapter.KEY_JUSTDATE, FixturesDbAdapter.KEY_JUSTTIME, FixturesDbAdapter.KEY_HOMETEAM, FixturesDbAdapter.KEY_AWAYTEAM, FixturesDbAdapter.KEY_LOCATION, FixturesDbAdapter.KEY_COMPETITION};
   int[] to2 = new int[] { R.id.fixtextlist, R.id.fixtextlistkotime, R.id.fixtextlisthome, R.id.fixtextlistaway, R.id.fixtextliststad, R.id.fixtextlistcomp};

   // Now create an array adapter and set it to display using our row
   ListView resultsView = (ListView) findViewById(R.id.list2);
   SimpleCursorAdapter results =
       new SimpleCursorAdapter(this, R.layout.fixlist_item2, c2, from2, to2);
   resultsView.setAdapter(results);


  }


Inevitably I answered my own question eventually :p

It seems that a standard Android ListView has it's state saved, however a custom ListView where the view has to be declared does not.

Therefore I simply ran the method from inside the onConfigurationChanged() method. This seems to work fine.

e.g.

  public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  populateCustomList();
  }

This is probably doing work that doesn't need to be done but hey, it solves the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜