The content of the adapter has changed but ListView did not receive a notification,listviewadapter

來源:互聯網
上載者:User

The content of the adapter has changed but ListView did not receive a notification,listviewadapter

在非Ui線程中修改ListView繫結資料對象的問題

問題描述:

       在非UI線程中修改了ListView繫結資料對象(如List)時,如下異常:


問題分析:

        在非UI線程下不能夠對主線程中ListView綁定的List資料進行修改。

解決方案:

方法一:將對List中資料的修改放到主線程中。

方法二:將子線程中要更新的資料通過Handler傳遞到主線程中,然後在主線程中將傳遞過來的資料添加到ListView繫結資料對象List中,然後同時adapter資料改變。

在這裡主要介紹第二種方式:

具體步驟:

1.在子線程中將要更新的資料以Message的方式傳遞給主線程。

Map<String, Object>item=new HashMap<String, Object>();//擷取程式的包名String packageName=appInfo.packageName;item.put("imgIco", ico);item.put("appName", appName);item.put("packageName", packageName);Message message = new Message();message.what =SCANNING;message.obj = item;mHandler.sendMessage(message); 

 

2.在主線程中通過Handler處理子線程發來的訊息,更新ListView的資料對象。

Handler mHandler=new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubswitch (msg.what) {case FLAG_LOAD_SUCCESS://完成掃描break;case SCANNING://正在掃描items.add((Map<String, Object>) msg.obj);//通知適配器資料改變adapter.notifyDataSetChanged();

 


listView經常出現這樣的問題,怎解決?

當你的listview中的資料發生改變時,必須通知adapter告訴它資料變了,方法是:
adapter.notifyDataSetChanged();

由於這個方法實際上是在更新UI,所以必須在UI線程也就是主線程裡進行。
 
android載入下一頁資料,壓力測試多次滑動報錯:The content of the adapter has changed

Adapter資料的更新必須在UI thread中來添加,並且添加完使用notify函數通知。或者貼一下源碼。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.