在Android中,可以通过以下几种方式刷新ListView的数据:
notifyDataSetChanged()方法:这是最简单的方法,在数据源发生变化后,调用ListView的notifyDataSetChanged()方法即可刷新数据。例如:listAdapter.notifyDataSetChanged(); notifyDataSetInvalidated()方法:当数据源发生较大的变化时,可以使用notifyDataSetInvalidated()方法来刷新数据。这个方法会清除ListView中的所有缓存,并重新绘制列表。例如:listAdapter.notifyDataSetInvalidated(); listView.setAdapter(newListAdapter); notifyDataSetChanged()方法刷新数据。例如:<androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { // 更新数据源 // 例如:dataList.clear(); dataList.add(...); // 刷新数据 listAdapter.notifyDataSetChanged(); // 停止刷新动画 swipeRefreshLayout.setRefreshing(false); } });