首页 > 代码库 > android ListView中使用notifyDataSetChanged()不刷新

android ListView中使用notifyDataSetChanged()不刷新

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();
}

 

android ListView中使用notifyDataSetChanged()不刷新