0

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
angel
  • 75
  • 1
  • 9
  • 1
    Well, could you also post the source code of your PlaeListAdapter too? And it is still question if your are using the single instance of `placeListFragment` for example or instantiate this fragment every time, when the bottomNavigationBar is clicked. – jantursky May 13 '18 at 07:18
  • `adapter.notifydatasetchange(); ` ? – Na-In Hae May 13 '18 at 07:19
  • i'm pretty much sure that is because you are not instantiating your fragment every time, that is why only when the fragment is destroyed the method "onCreateView" is called, and refreshing the adapter – Gal Yedidovich May 13 '18 at 08:33
  • another guess is that the problem is not with the fragment but the adapter, or your DAL. you should debug it to see that data is accurate – Gal Yedidovich May 13 '18 at 09:43

2 Answers2

0

Instead of placeListAdapter.refreshListAdapter() you should use placeListAdapter.notifydatasetchange() whenever you are updating the recyclerview data.

Amit
  • 25
  • 2
  • Thank you for replaying. Tried your suggestion, there was no difference. Maybe something extra needs to be done ? – angel May 13 '18 at 08:12
0

I cant be sure without knowing if you are instantiating the fragment once or more, but I'm pretty sure that is because the life cycle of the fragment (in case you only instantiate once)

override 'onStart' method in your fragment refresh the adapter in onStart method:

@Override
public void 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.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); //keep the recycler view as field in fragment calls

    //refresh your adapter
    placeListAdapter.notifyDataSetChanged();
    //or as you already wrote
    placeListAdapter.refreshListAdapter();
}

another option:

please try to instantiate your fragment every time that you do a transaction to it.

private void showMap(){
    constraintMap.setVisibility(View.VISIBLE);

    emptyFragment = new EmptyFrag(); //instantiating

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

    placeListFragment = new YourFragment(); //instantiating

    fragmentManager
            .beginTransaction()
            .replace(container, placeListFragment)
            .commit();

}//showHistory

private void showNear(){
    Bundle bundle = new Bundle();
    bundle.putInt(Util.BOTTOM_NAVIGATOR, Util.NEAR);
    placeListFragment.setArguments(bundle);

    placeListFragment = new YourFragment(); //instantiating   

    constraintMap.setVisibility(View.GONE);

    fragmentManager
            .beginTransaction()
            .replace(container, placeListFragment)
            .commit();

}//showNear
Gal Yedidovich
  • 649
  • 7
  • 14