notifyDataSetChanged在适配器上不起作用

时间:2018-10-23 09:15:38

标签: java android

我在ViewPage中有活动和3个片段。

在最后一个片段中,我有recycleView,如果我移至此页面,则仅当我致电时才想刷新回收视图及其工作:

https://xxx/rest/greenhopper/1.0/sprintquery/22407?includeFutureSprints=true&includeHistoricSprints=false
https://xxx/rest/greenhopper/1.0/rapid/charts/sprintreport?rapidViewId=22407&sprintId=34312
https://xxx/rest/greenhopper/1.0/rapid/charts/scopechangeburndownchart?rapidViewId=22407&sprintId=34312
https://xxx/rest/greenhopper/1.0/rapid/charts/velocity.json?rapidViewId=22407

在调用notifyDataSetChanged之后,它应该也可以正常工作,但没有成功。 我们该怎么办?

最后一个片段:

 mAdapter = new LocationAdapter(mListener.loadLocationList());
        mRecyclerView.setAdapter(mAdapter);

我想要@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity()); mRecyclerView.setLayoutManager(mLayoutManager); mAdapter = new LocationAdapter(mListener.loadLocationList()); mRecyclerView.setAdapter(mAdapter); } private class HistoryFragmentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (HISTORY_FRAGMENT_SELECTED.equals(intent.getAction())){ updateLocationList(); } } } private void updateLocationList() { mAdapter = new LocationAdapter(mListener.loadLocationList()); mRecyclerView.setAdapter(mAdapter); } 而不是notifydatasetchanged中的2行

LocationAdapter:

updatelocationlist()

1 个答案:

答案 0 :(得分:0)

问题是您只能在适配器的构造函数中设置数据。
当您致电notifyDataSetChanged时,不能指望适配器会神奇地获取新数据。您必须自己设置。 我的建议是在适配器中添加setData方法,并在notifyDataSetChanged之前调用它。
在您的适配器中是这样的:

public void setData(LinkedList<LatLng> myDataset) {
    mDataset = myDataset
}

和您的呼叫者中

private void updateLocationList() {
    mAdapter.setData(mListener.loadLocationList());
    mAdapter.notifyDataSetChanged();
}