我有一个ListView
, 从 XML 填充。一切正常,但是当我更改设备的方向(纵向<-->横向)时,它会重新加载ListView
. 我想阻止它。我用过 onSaveInstanceState
和onConfigurationChanged
。但它仍然重新加载。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
alrowDataView = savedInstanceState.getParcelableArrayList("name");
// Toast.makeText(MainActivity.this,alrowDataView.size() , Toast.LENGTH_LONG).show();
// lvrowDataView=(ListView) findViewById(R.id.list_xml);
}
else{
Toast.makeText(MainActivity.this,"NOdata" , Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
lvrowDataView = (ListView) findViewById(R.id.list_xml);
ParsingXml parsing = new ParsingXml();
在一般 ListView 操作之后......
这是和的OnSavedInstanceState
代码onConfigurationChanged
。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(MainActivity.this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(MainActivity.this, "portrait", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putParcelableArrayList("name", alrowDataView);
//Toast.makeText(MainActivity.this, alrowDataView.size(),Toast.LENGTH_LONG).show();
}
这些是声明
private ListView lvrowDataView;
private ArrayList<RowData> alrowDataView;
RowDataAdapter adapterRowData;
我不想用android:configChanges="orientation"
另外,在onSaveInstanceState
ArrayList 中为空!!!