android ListView中使用notifyDataSetChanged()不刷新

时间:2022-05-23 19:36:31

private List<Navi> mRouteList;

1. 设置Adapter

private void initData() {
mRouteList
= new ArrayList<Route>();
mRouteList.clear();
mAdapter
= new CarRouteAdapter(getActivity(), mRouteList);
mListView.setAdapter(mAdapter);
}

2. 获取数据,刷新listview

 

private void updateRoutes(List<MtqCarRoute> listOfResult) {
  mRouteList.clear();  mRouteList = CarUtils.formatNaviRoutes(listOfResult);
  mAdapter.notifyDataSetChanged();

}

发现这样做,listview的数据已经发生变化,但是就是没有更新。

解决如下:

private void updateRoutes(List<MtqCarRoute> listOfResult) {
  mRouteList.clear();
  
// mRouteList = CarUtils.formatNaviRoutes(listOfResult);
mRouteList.addAll(CarUtils.formatNaviRoutes(listOfResult));
  mAdapter.notifyDataSetChanged();
}