2014-03-19 52 views
1

下面是代碼更新列表:刷新ListAdapter上的onResume()與數據庫更新不工作

@Override 
public void onResume() { 
    super.onResume(); 
    adapter.notifyDataSetChanged(); 

    Log.d("onResumeMethod", "It is called! :)"); 
} 
} 

我用這東西時,在數據庫中進行編輯,刷新列表。不幸的是,它不工作。我讀過的某處我應該使用重新查詢,但它已被棄用。有什麼建議?

編輯:

已經這樣嘗試過,但沒有幫助(不幸的是,因爲我確信這是要去工作):

@Override 
public void onResume() { 
    super.onResume(); 
    adapter = new ArrayAdapter<Friend>(this, 
      android.R.layout.simple_list_item_1, list); 
    this.getListView().setAdapter(adapter); 
    this.getListView().invalidate(); 
} 

我發現了另外一個問題:如果我使用後退按鈕(上仿真器)返回到桌面,並嘗試通過點擊菜單項來打開應用就說明我:

03-19 15:43:50.153: E/AndroidRuntime(7374): FATAL EXCEPTION: main 
03-19 15:43:50.153: E/AndroidRuntime(7374): java.lang.RuntimeException: Unable to start  
activity  
ComponentInfo{com.example.birthdayreminder/com.example.birthdayreminder.MainActivity}: 
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 
'android.R.id.list' 

我面對這個問題之前,讓他們勸我插入這個我的xml:

<ListView 
android:id="@android:id/list" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" > 
</ListView> 

回答

2

您需要再次從數據庫中獲取數據。然後你就可以在你的列表視圖重新設置適配器,並呼籲invalidate(),像下面的方法:

@Override 
public void onResume() { 
    super.onResume(); 

    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, db.getMyData()); 

    myListView.setAdapter(adapter); 

    myListView.invalidate(); 
} 

哪裏db.getMyData()是你的方法從數據庫中獲取數據,並myListView是你ListView

希望它有幫助!

+0

THX的回答,請檢查我的編輯:) –

+0

更改機器人:ID = 「@機器人:ID /列表」 到Android:ID = 「@ + ID /列表」 。你可以檢查它在這篇文章http://stackoverflow.com/questions/5025910/difference-between-id-and-id-in-android – Giacomoni

+0

同樣的錯誤顯示,在一個logCat。 –

0

覆蓋notifyDataSetChanged

@Override 
public void notifyDataSetChanged() { 
    super.notifyDataSetChanged(); 

    myArray.clear(); 
    /* load the data again */ 
    myArray = yourMethodToLoadData(); 
}