简体   繁体   中英

notifyDataSetChanged didn't work on adapter

I have activity and 3 fragments in ViewPage.

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:

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

This should work aswell after call notifyDataSetChanged but didn't. 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()

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. You have to set it by yourself. My suggestion would be add a setData method in your adapter and call it before 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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