Reputation: 3471
I've created a simple RecyclerView.Adapter
and it displays the content properly, but when I call notifyDataSetChanged()
on it - it doesn't work.
PastEventFragment
public class PastEventFragment extends Fragment {
private RecyclerView mRecyclerView;
private List<PastEventItem> mPastEventItemList;
private PastEventAdapter mPastEventAdapter;
public PastEventFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View pastEvent = inflater.inflate(R.layout.fragment_past_event, container, false);
mRecyclerView = (RecyclerView) pastEvent.findViewById(R.id.pe_rv);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mPastEventItemList = PastEventItem.listAll(PastEventItem.class);
mPastEventAdapter = new PastEventAdapter(mPastEventItemList, getActivity());
mRecyclerView.setAdapter(mPastEventAdapter);
return pastEvent;
}
public void refreshAdapter() {
mPastEventItemList.clear();
mPastEventItemList = PastEventItem.listAll(PastEventItem.class);
mPastEventAdapter.notifyDataSetChanged();
}
}
Adapter:
public class PastEventAdapter extends RecyclerView.Adapter<PastEventAdapter.PastEventHolder> {
private List<PastEventItem> pastEventItemList;
private Context mContext;
public PastEventAdapter(List<PastEventItem> pastEventItemList, Context context) {
this.pastEventItemList = pastEventItemList;
this.mContext = context;
}
@Override
public int getItemCount() {
return pastEventItemList.size();
}
@Override
public void onBindViewHolder(PastEventHolder pastEventHolder, int i) {
PastEventItem pei = pastEventItemList.get(i);
switch (pei.type) {
case 1:
pastEventHolder.type.setText(mContext.getResources().getString(R.string.string1));
break;
case 2:
pastEventHolder.type.setText(mContext.getResources().getString(R.string.string2));
break;
case 3:
pastEventHolder.type.setText(mContext.getResources().getString(R.string.string3));
break;
case 4:
pastEventHolder.type.setText(mContext.getResources().getString(R.string.string4));
break;
}
pastEventHolder.ml.setText(Integer.toString(pei.ml));
pastEventHolder.per.setText(Integer.toString(pei.per));
}
@Override
public PastEventHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.past_event_item, viewGroup, false);
return new PastEventHolder(itemView);
}
public static class PastEventHolder extends RecyclerView.ViewHolder {
private TextView type;
private TextView ml;
private TextView per;
public PastEventHolder(View v) {
super(v);
type = (TextView) v.findViewById(R.id.pe_type);
ml = (TextView) v.findViewById(R.id.pe_ml);
per = (TextView) v.findViewById(R.id.pe_per);
}
}
}
I call refreshAdapter
method from another Fragment
(DialogFragment
) through an Activity, using Interface
. I used a debugger, so I know that the refreshAdapter
method is called. It just has no impact on the RecyclerView which is displayed. When I leave the PastEventFragment
and then switch back to it, so that the onCreateView
method is called, all previously added items are shown, so there is no problem with the ORM.
Upvotes: 2
Views: 845
Reputation: 15929
Your adapter has a reference to mPastEventItemList
. However, you are creating a new List (a new reference) in this line of code:
mPastEventItemList = PastEventItem.listAll(PastEventItem.class);
but your adapter is still connected to the old reference of mPastEventItemList
.
So you should do something like that instead:
public void refreshAdapter() {
mPastEventItemList.clear();
mPastEventItemList.addAll(PastEventItem.listAll(PastEventItem.class));
mPastEventAdapter.notifyDataSetChanged();
}
Upvotes: 2
Reputation: 1709
Try to add a setItemList()
method in the adapter. And set the updated list data using this method with the adapter, after this, call the notifyDatasetChange method
Upvotes: 0