Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

「The content of the adapter has changed」錯誤怎麼解

編輯:進階開發

新手在做 Android 開發時,常對 Thread、Message、Handler 與 Looper 的關係不太清楚,於是容易碰到這類錯誤:

The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread but only from the UI thread.

錯誤訊息已經把答案寫出來了

意思是:你在背景執行緒中修改了 Adapter 的內容,但 ListView 並沒有收到通知,導致它顯示的資料與實際不符。

兩個處理方向

一、把修改動作搬回 UI 執行緒。Adapter 內容的變更必須在 UI 執行緒中進行。實作上可以透過 Handler 把訊息拋回主執行緒處理,這是 Android 中最典型的執行緒溝通方式。

二、明確發出通知。在資料變更後呼叫 Adapter 的 notifyDataSetChanged(),讓 ListView 知道該重新綁定。不過具體要不要呼叫、在哪裡呼叫,仍得依你的程式結構而定。

如果能提供更具體的程式碼,通常可以給出更精準的診斷,但這兩點已能涵蓋絕大多數案例。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved