简体   繁体   English

notifyDataSetChanged在适配器上不起作用

[英]notifyDataSetChanged didn't work on adapter

I have activity and 3 fragments in ViewPage. 我在ViewPage中有活动和3个片段。

In last fragment I have recycleView, if i move to this page i want to refresh recycle view and its work only if I call: 在上一个片段中,我具有recycleView,如果我移至此页面,则仅当我致电时才想刷新回收视图及其工作:

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

This should work aswell after call notifyDataSetChanged but didn't. 调用notifyDataSetChanged之后,它应该也可以正常工作,但是没有成功。 What can we wrong ? 我们能做错什么?

in last fragment: 在最后一个片段中:

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

i wanted notifydatasetchanged instead of 2 lines in updatelocationlist() 我想要notifydatasetchanged而不是updatelocationlist()的2行

LocationAdapter: LocationAdapter:

public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.LocationViewHolder> {

    private LinkedList<LatLng> mDataset;

    public static class LocationViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public View mTextView;
        public LocationViewHolder(View v) {
            super(v);
            mTextView = v;
        }
    }

    public LocationAdapter(LinkedList<LatLng> myDataset) {
        mDataset = myDataset;
    }

    @NonNull
    @Override
    public LocationAdapter.LocationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View v =  layoutInflater.inflate(R.layout.my_text_view, parent, false);
        LocationViewHolder vh = new LocationViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull LocationAdapter.LocationViewHolder locationViewHolder, int position) {
       TextView textView = locationViewHolder.mTextView.findViewById(R.id.textView3);
       textView.setText(String.valueOf(mDataset.get(position)));

    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }
}

The problem is you set data only in your adapter's constructor. 问题是您只能在适配器的构造函数中设置数据。
When you call notifyDataSetChanged you can't expect that your adapter will take new data magically. 当您调用notifyDataSetChanged您不能指望适配器会神奇地获取新数据。 You have to set it by yourself. 您必须自己设置。 My suggestion would be add a setData method in your adapter and call it before notifyDataSetChanged . 我的建议是在适配器中添加setData方法,并在notifyDataSetChanged之前调用它。
Something like this in your adapter: 适配器中的内容如下:

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

and in your caller: 并在您的呼叫者中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM