我有一个ListView
,从XML填充。一切正常,但是当我改变设备的方向(Portrait< - > Landscape)时,它会重新加载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
中,ArrayListis为空!!!“
答案 0 :(得分:0)
试试这种方式
android:configChanges="orientation"
不使用最新的android api 3.0 +
你需要像这样使用
<activity
android:name="YourActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
/>