I have looked at this link and others: How to update RecyclerView Adapter Data?
And could not find a solution to an issue I am having. On some occasions the RecyclerView will update its data and on others it won’t.
In my code I am using a bottom navigator, and updating the data accordingly. When I select MAP and then HISTORY, or MAP and then NEAR at the bottom navigator, the data will update and will work just fine. If I chose HISTORY and then NEAR the data will NOT update. I have played with this, tried several options, and could not get it to work.
This is the code from the bottom navigator:
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case (R.id.action_History):
showHistory();
break;
case (R.id.action_near):
showNear();
break;
case (R.id.action_fav):
showFav();
break;
case (R.id.action_map):
showMap();
break;
default:
break;
}
return true;
}
});
These are the functions called from the bottom navigator:
private void showMap(){
constraintMap.setVisibility(View.VISIBLE);
fragmentManager
.beginTransaction()
.replace(container, emptyFragment)
.commit();
}//showMap
private void showHistory(){
Bundle bundle = new Bundle();
bundle.putInt(Util.BOTTOM_NAVIGATOR, Util.HISTORY);
placeListFragment.setArguments(bundle);
constraintMap.setVisibility(View.GONE);
fragmentManager
.beginTransaction()
.replace(container, placeListFragment)
.commit();
}//showHistory
private void showNear(){
Bundle bundle = new Bundle();
bundle.putInt(Util.BOTTOM_NAVIGATOR, Util.NEAR);
placeListFragment.setArguments(bundle);
constraintMap.setVisibility(View.GONE);
fragmentManager
.beginTransaction()
.replace(container, placeListFragment)
.commit();
}//showNear
This is the onCreateView from the fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_list_view, container, false);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
placesRecyclerView = view.findViewById(R.id.location_recycler_view);
placesRecyclerView.setLayoutManager(linearLayoutManager);
placesRecyclerView.setItemAnimator(new DefaultItemAnimator());
int bottomNavigator;
if(getArguments() != null){
bottomNavigator = getArguments().getInt(Util.BOTTOM_NAVIGATOR);
switch (bottomNavigator){
case Util.HISTORY:
placeLocations = dal.getAllPlaceLocationSearchResults();
break;
case Util.NEAR:
placeLocations = dal.getAllPlaceLocationNearResults();
break;
case Util.FAVORITE:
placeLocations = dal.getAllPlaceLocationFavoritesResults();
break;
case Util.MAP:
break;
}
} else{
placeLocations = dal.getAllPlaceLocationSearchResults();
}
placeListAdapter = new PlaceListAdapter(placeLocations, listener, mContext);
placesRecyclerView.setAdapter(placeListAdapter);
placeListAdapter.refreshListAdapter();
return view;
}//onCreateView
As suggested by Gal Yedidovich. I have overwritten onStart. But it did not help.
@Override
public void onStart() {
super.onStart();
int bottomNavigator;
if(getArguments() != null){
bottomNavigator = getArguments().getInt(Util.BOTTOM_NAVIGATOR);
switch (bottomNavigator){
case Util.HISTORY:
placeLocations = dal.getAllPlaceLocationSearchResults();
break;
case Util.NEAR:
placeLocations = dal.getAllNearLocations();
break;
case Util.FAVORITE:
placeLocations = dal.getAllPlaceLocationFavoritesResults();
break;
case Util.MAP:
break;
}
} else{
placeLocations = dal.getAllPlaceLocationSearchResults();
}
placeListAdapter = new PlaceListAdapter(placeLocations, listener, mContext);
placesRecyclerView.setAdapter(placeListAdapter);
placeListAdapter.refreshListAdapter();
}//onStart