android關於清除listView(removeAllView)的所有子view的方法
阿新 • • 發佈:2019-02-01
今天做專案遇到了一個問題,從Activity B返回到Activity A時資料已經更新了,但是Activity A顯示的依然是老資料(資料條目從2條變成1條的時候)。
我意識到是UI的問題,想呼叫listView的removeAllView方法,結果App就爆掉了。
看了下原始碼,原來removeAllView裡寫的是這樣:
<span style="font-size:18px;">/** * This method is not supported and throws an UnsupportedOperationException when called. * * @throws UnsupportedOperationException Every time this method is invoked. */ @Override public void removeAllViews() { throw new UnsupportedOperationException("removeAllViews() is not supported in AdapterView"); }</span>
但是我程式碼裡已經有把adapter進行notifyDataSetChanged();了,說明是listView的資料沒有來得及更新,接著我把listView再set一次adapter,問題就解決了。
<span style="font-size:18px;">@Override
protected void onStart() {
super.onStart();
initBagData();
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
};</span>