EDIT: This problem may be related to http://stackoverflow.com/questions/3121153/baseadapter-causing-listview-to-go-out-of-order-when-scrolled
My main application screen uses a GridView to display a grid of icons. Each icon has an image, name, and Intent. I have the Activity set to handle orientation changes so I can change the background. The onCreate method sets up the GridView with 3 test icons that don't do anything and a preferences icon which opens a PreferencesActivity:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gv = (GridView) findViewById(R.id.main_grid);
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE)
gv.setBackgroundResource(R.drawable.main_bg_horiz);
else
gv.setBackgroundResource(R.drawable.main_bg);
ArrayList<Icon> icons = new ArrayList<Icon>();
icons.add(new Icon("Test 1", R.drawable.test1, null));
icons.add(new Icon("Test 2", R.drawable.test2, null));
icons.add(new Icon("Test 3", R.drawable.test3, null));
icons.add(new Icon("Preferences", R.drawable.perferences, new Intent(this,
AlertPreferences.class)));
IconAdapter ia = new IconAdapter(icons);
gv.setAdapter(ia);
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
Intent i = ((IconAdapter)arg0.getAdapter()).getItem(arg2).getIntent();
if (i != null)
HomeActivity.this.startActivity(i);
}
});
And the onConfigurationChanged is very simple:
super.onConfigurationChanged(newConfig);
GridView gv = (GridView) findViewById(R.id.main_grid);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
gv.setBackgroundResource(R.drawable.main_bg_horiz);
else
gv.setBackgroundResource(R.drawable.main_bg);
The background changes just fine but the code introduces a new problem with the GridView. Whatever orientation I start the application in it works just fine. However, when I change orientation, the order of the icons in the GridView reverses. And when I rotate the screen back to the orientation it started in, everything goes back to normal. What's going on here?