[英]Android listview disappears after screen rotation
在我的布局上,我有一些按鈕做出一些選擇,然后一個按鈕來執行對數據庫的查詢。 此查詢的結果顯示在此布局中的ListView
。
問題是如果在我執行查詢后旋轉屏幕, ListView
消失並且必須再次執行查詢。
我相信這種情況正在發生,因為活動重新開始。 按照這里的建議,我已經在清單android:configChanges="orientation|keyboardHidden"
中添加了我的活動,並在我的代碼中添加了:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.mylayout);
}
但這不起作用。
這是我的活動的完整代碼:
public class MyClass extends ListActivity implements OnClickListener, OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
// Creates the buttons and setOnClickListener and setOnCheckedChangeListener
}
@Override
public void onClick(View v) {
// Manages the buttons and their functions
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// See what group in radio group is checked
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// After pressing one button, a query is made and a listview is shown.
// This it to handle the user choice after he clicks an item on the listview
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.mylayout);
}
}
這很奇怪,因為我還有其他一些活動:
public class AtoZ extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.atoz);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
}
這也會對數據庫執行查詢,在ListView
上顯示它,然后處理用戶選擇。 如果我旋轉屏幕, ListView
仍會顯示。
我能做什么?
在清單文件下的活動中使用configChanges。
<activity
...
...
android:configChanges="orientation|screenSize|keyboard">
</activity
這對我有用。
有兩件事可以解決你的問題:
求解器A:
android:configChanges="orientation|keyboardHidden"
。 onConfigurationChanged(Configuration newConfig)
函數。 因為每次初始化Activity時都只調用onCreate()
一次。 當你的onConfigurationChanged()
用空數據重新加載和膨脹新視圖R.layout.mylayout
=>你的列表不會綁定數據。
求解器B:
將已在onCreate()中編寫的數據綁定代碼移動 到重寫方法onStart()
或onResume()
。 我建議您使用onStart()
來處理數據綁定。
您應該看到開發頁面Android Activity life cycle
(更新):
因為在單擊列表項上的搜索按鈕時加載listContent
,所以必須維護活動中的數據以從中重建列表(例如:搜索字符串)。 然后在onStart()
您可以從此數據重建列表。
但是,單擊List的項目以更改整個List本身時,您的邏輯很奇怪。 請注意:單擊列表項上的按鈕時,也會觸發onListItemClick(..)
。
在onConfigurationChanged方法中執行setContentView時,將重新創建ListView。 您需要再次將數據加載到ListView中。 如果要將ListView綁定到適配器,則需要在onConfigurationChanged中執行此操作。
您必須覆蓋onSaveInstanceState
和onRestoreInstanceState
在onSaveInstanceState
保存Bundle
上的ListView
列表。
在onRestoreInstanceState
,使用Bundle
還原列表並重新創建適配器。
private ArrayList<String> data;
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putSerializable("d", data);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState != null) {
data = (ArrayList<String>) savedInstanceState.getSerializable("d");
ListViewAdapter adapter = new ListViewAdapter(this, data);
ListView listView = (ListView) findViewById(R.id.layout_listview);
listView.setAdapter(adapter);
}
super.onRestoreInstanceState(savedInstanceState);
}
當您進行方向更改時,您將不得不重做查詢(如果不保存它),因為完成此操作后,將再次調用oncreate。
您可以嘗試存儲列表視圖中的數據,例如共享首選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.