Comparator when sorting affects all lists

0

In short it would be:

public class TabProchesFragment extends Fragment   {

    private static final String TAG = "TabProchesFragment";

    private RecyclerView mRecyclerView;
    private RecyclerViewAdapter_Restaurant_MasProximos mRecyclerViewAdapterAccueil;
    private LinearLayoutManager layoutManager;
    List<Restaurant> restauranes = new ArrayList<>();
    Double latitud;
    Double longitud;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.tabproches_fragment,container,false);

       Bundle bundle = getArguments();

       layoutManager = new GridLayoutManager(getContext(),2);
       mRecyclerView = (RecyclerView) view.findViewById(R.id.rvRestaurantsProches);
       mRecyclerView.setLayoutManager(layoutManager);

       restauranes= (List<Restaurant>) bundle.getSerializable("restauranes");


       if (restauranes!=null) {
            latitud = bundle.getDouble("latitud");
            longitud = bundle.getDouble("longitud");
            Collections.sort(restauranes, new Comparator<Restaurant>() {
                @Override
                public int compare(Restaurant o1, Restaurant o2) {
                    return o1.tomarDistancia(latitud,longitud).compareTo(o2.tomarDistancia(latitud,longitud));
                }
           });
       }

       mRecyclerViewAdapterAccueil = new RecyclerViewAdapter_Restaurant_MasProximos(restauranes,latitud,longitud);
       mRecyclerView.setAdapter(mRecyclerViewAdapterAccueil);

       return view;
    }

}

And the other:

public class TabPromotionFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private RecyclerViewAdapter_Restaurant_MejorPromocion mRecyclerViewAdapterAccueil;
    private LinearLayoutManager layoutManager;
    List<Restaurant> restauranes = new ArrayList<>();
    Double latitud;
    Double longitud;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tabpromotion_fragment,container,false);
        Bundle bundle = getArguments();

        restauranes= (List<Restaurant>) bundle.getSerializable("restauranes");
        if (restauranes!=null) {
            latitud = bundle.getDouble("latitud");
            longitud = bundle.getDouble("longitud");
            Collections.sort(restauranes, new Comparator<Restaurant>() {
                @Override
                public int compare(Restaurant o1, Restaurant o2) {
                     return o2.tomarPromocion() - o1.tomarPromocion();
                }
            });
        }

        layoutManager = new GridLayoutManager(getContext(),2);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rvRestaurantsPromotion);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerViewAdapterAccueil = new RecyclerViewAdapter_Restaurant_MejorPromocion(restauranes,latitud,longitud);
        mRecyclerView.setAdapter(mRecyclerViewAdapterAccueil);

        return view;
    }

}

I have these two fragments, I pass the list that I get to a list within the fragment and then I sort the list with a Comparator, the problem is that what I order in the first one, it is ordered in the second, but they do not share the same adapter, nor becomes a duplicate fragment, are two fragments that are shown well, but the list that happened to them ends up being ordered as one of the 2 forms. But not of the 2 forms separately. I'm ordering one by distance and another by promotion.

Both are ordered by distance.

I put more code on how I pass the data if needed:

restauranes=rowListItem;
RestauranesPageAdapter adapter = new RestauranesPageAdapter(getSupportFragmentManager());
abProchesFragment tabProchesFragment = new TabProchesFragment();
TabPromotionFragment tabPromotionFragment= new TabPromotionFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("restauranes", (Serializable) rowListItem);
bundle.putDouble("latitud",latitud);
bundle.putDouble("longitud",longitud);
tabProchesFragment.setArguments(bundle);
tabPromotionFragment.setArguments(bundle);

adapter.addFragment(tabProchesFragment);
adapter.addFragment(tabPromotionFragment);
mViewPager.setAdapter(adapter);
    
asked by Eduardo Ricardez 30.04.2017 в 15:39
source

0 answers