show the most active items in recyclerview in order of their location?

You need to separate the population of markerObjList and location change listener. In ‘onResponse’ method only populate the markerObject list.

create the location change listener in separate method say onCreate of view and when location listener is triggered, recalculate the distances for all marker object, sort the list based on updated distance and set the sorted list to adapter. The code will look something like this

onCreate()
   {
       LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
       int finalI = i;
       LocationListener locationListener = new LocationListener() {
           @Override
           public void onLocationChanged(Location location) {

               double latView = location.getLatitude();
               double lonView = location.getLongitude();

               try {
               
                   ArrayList<> newMarkerObjList = new ArrayList<>();
                   int i = 0;
                   // recalcualte distances of all marker and add them in new list
                   for( i =0; i < markerObjList.size()){
                   
                       MarkerObj   markerObj = markerObjList[i]
                       Location loc1 = new Location(markerObj.getName());
                       loc1.setLatitude(Double.parseDouble(markerObj.getLat()));
                       loc1.setLongitude(Double.parseDouble(markerObj.getLon()));

                       Location loc2 = new Location("");
                       loc2.setLatitude(latView);
                       loc2.setLongitude(lonView);

                       float distanceInMeters = loc1.distanceTo(loc2);
                       String distanceView = String.valueOf(distanceInMeters);

                       MarkerObj markerObj1 = new MarkerObj(
                               markerObj.getId(),
                               markerObj.getName(),
                               markerObj.getRef(),
                               markerObj.getLat(),
                               markerObj.getLon(),
                               markerObj.getImages(),
                               markerObj.getAlimentation(),
                               markerObj.getWifi(),
                               markerObj.getFastfood(),
                               markerObj.getLavage(),
                               markerObj.getEntretien(),
                               distanceInMeters,
                               distanceView);

                       newMarkerObjList.add(markerObj1);
                   }
                   // create a predicate for sorting based on 'distanceInMeters';
                   Collections.sort( newMarkerObjList); 
                   markerObjList = newMarkerObjList;
                   // and set this new list to the adapter;
                   NosStations.AdapterListeStations adapterListeStations = new NosStations.AdapterListeStations(getApplicationContext(), markerObjList);
                   recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                   recyclerView.setAdapter(adapterListeStations);
                   recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), LinearLayoutManager.VERTICAL));
                   recyclerView.setHasFixedSize(true);
                   adapterListeStations.notifyDataSetChanged();

                   progressBar.setVisibility(View.GONE);

               }
               catch (Exception e){
                   e.printStackTrace();
                   Log.e("error", e.toString());
               }
           }

           @Override
           public void onStatusChanged(String provider, int status, Bundle extras) {
           }

           @Override
           public void onProviderEnabled(String provider) {

           }

           @Override
           public void onProviderDisabled(String provider) {

           }
       };
   }

Current location listener is added for each element in array and the a updated marker object is added in same list , hence resulting in duplicate markers in the list and increasing the size of marker list

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top