0

I have calculated distance between two locations inside adapter but now I cannot sort the RecyclerView based on distance. Because the calculated distance is not from helper class.

Following is the Code from Adapter Class:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;
    ArrayList<JobSeekerClass> list;
    Double JSlat, JSlng;
    LocationManager locationManager;
    String provider;

    public MyAdapter(Context context, ArrayList<JobSeekerClass> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        provider = locationManager.getBestProvider(new Criteria(), false);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        Location location = locationManager.getLastKnownLocation(provider);
        JobSeekerClass JobSeeker = list.get(position);
        //Job Seeker Lat Lng
        JSlat= location.getLatitude();
        JSlng= location.getLongitude();
        LatLng Job_Seeker= new LatLng(JSlat, JSlng);
        //Job Provider Lat Lng
        String JPlat= JobSeeker.getJplongitude();
        String JPlng= JobSeeker.getJplatitude();
        Double JP_lat= Double.parseDouble(JPlat);
        Double JP_lng= Double.parseDouble(JPlng);
        LatLng Job_Provider= new LatLng(JP_lat, JP_lng);
        //Calculating Distance
        Double Distance= SphericalUtil.computeDistanceBetween(Job_Seeker, Job_Provider);
        //Distance in Kilometers
        Double Distance_in_KM= Distance/1000;
        Double dist= Math.floor(Distance_in_KM);
        String distance_string= dist.toString();
        
        //Setting Text for Calculated Distance in RecyclerView
        holder.faasla_Digit.setText(distance_string);
        
        //Setting Text for JobSeeker City
        holder.City.setText(JobSeeker.getJPlocation());
        holder.PhoneNumber.setText(JobSeeker.getJPphone_no());
        holder.find_job_Location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + list.get(position).getJplongitude() + "," + list.get(position).getJplatitude() + "&mode=l"));
                intent.setPackage("com.google.android.apps.maps");
                if (intent.resolveActivity(context.getPackageManager()) != null) {
                    context.startActivity(intent);
                }          
            }
        });
        //Salary Digit Sound for corresponding values
        holder.Salary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.Salary.getText().equals("1000")) {
                    MediaPlayer mplayer = MediaPlayer.create(context.getApplicationContext(), R.raw.aikhazar);
                    mplayer.start();
                }
            }
        });
    }
    @Override
    public int getItemCount() {
        return list.size();
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder implements LocationListener {
        TextView Salary, City, PhoneNumber, city_text, phone_number_text, salary_text,
                raasta_maloom_karain, faasla_Digit;
        Button find_job_Location;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            City = itemView.findViewById(R.id.tvJobLocation);
            PhoneNumber = itemView.findViewById(R.id.tvPhoneNo);
            Salary = itemView.findViewById(R.id.tvSalary);
            city_text = itemView.findViewById(R.id.Sheher);
            phone_number_text = itemView.findViewById(R.id.raabta_number);
            salary_text = itemView.findViewById(R.id.mahana_tankhuwa);
            raasta_maloom_karain = itemView.findViewById(R.id.raasta_maloom_karain);
            find_job_Location = itemView.findViewById(R.id.find_job_location);
            faasla_Digit = itemView.findViewById(R.id.faasla_digit);
        }

        @Override
        public void onLocationChanged(@NonNull Location location) {

        }

        @Override
        public void onLocationChanged(@NonNull List<Location> locations) {
            LocationListener.super.onLocationChanged(locations);
        }

        @Override
        public void onFlushComplete(int requestCode) {
            LocationListener.super.onFlushComplete(requestCode);
        }

        @Override
        public void onProviderEnabled(@NonNull String provider) {
            LocationListener.super.onProviderEnabled(provider);
        }

        @Override
        public void onProviderDisabled(@NonNull String provider) {
            LocationListener.super.onProviderDisabled(provider);
        }
    }
}

Following is the Code from Helper Class:

public class JobSeekerClass {
    String JPfullname, JPphone_no, JPemail, JPaddress, JPjob_title,
            JPlocation, JPsalary, JPDMwages, JPFNwages, jplongitude, jplatitude;

    public JobSeekerClass()
    {

    }
    public JobSeekerClass(String JPfullname, String JPphone_no, String JPemail, String JPaddress,
                          String JPjob_title, String JPlocation, String JPsalary, String JPDMwages,
                          String JPFNwages, String jplongitude, String jplatitude)
    {
        this.JPfullname= JPfullname;
        this.JPphone_no= JPphone_no;
        this.JPemail= JPemail;
        this.JPaddress= JPaddress;
        this.JPjob_title= JPjob_title;
        this.JPlocation= JPlocation;
        this.JPsalary= JPsalary;
        this.JPDMwages= JPDMwages;
        this.JPFNwages= JPFNwages;
        this.jplongitude= jplongitude;
        this.jplatitude= jplatitude;
    }


    public String getJplongitude() {
        return jplongitude;
    }

    public void setJplongitude(String jplongitude) {
        this.jplongitude = jplongitude;
    }

    public String getJplatitude() {
        return jplatitude;
    }

    public void setJplatitude(String jplatitude) {
        this.jplatitude = jplatitude;
    }

    public String getJPfullname() {
        return JPfullname;
    }

    public void setJPfullname(String JPfullname) {
        this.JPfullname = JPfullname;
    }

    public String getJPphone_no() {
        return JPphone_no;
    }

    public void setJPphone_no(String JPphone_no) {
        this.JPphone_no = JPphone_no;
    }

    public String getJPemail() {
        return JPemail;
    }

    public void setJPemail(String JPemail) {
        this.JPemail = JPemail;
    }

    public String getJPaddress() {
        return JPaddress;
    }

    public void setJPaddress(String JPaddress) {
        this.JPaddress = JPaddress;
    }

    public String getJPjob_title() {
        return JPjob_title;
    }

    public void setJPjob_title(String JPjob_title) {
        this.JPjob_title = JPjob_title;
    }

    public String getJPlocation() {
        return JPlocation;
    }

    public void setJPlocation(String JPlocation) {
        this.JPlocation = JPlocation;
    }

    public String getJPsalary() {
        return JPsalary;
    }

    public void setJPsalary(String JPsalary) {
        this.JPsalary = JPsalary;
    }

    public String getJPDMwages() {
        return JPDMwages;
    }

    public void setJPDMwages(String JPDMwages) {
        this.JPDMwages = JPDMwages;
    }

    public String getJPFNwages() {
        return JPFNwages;
    }

    public void setJPFNwages(String JPFNwages) {
        this.JPFNwages = JPFNwages;
    }

    public String toString()
    {
        return this.JPlocation + "";
    }
}

Now I(JobSeeker) have fetched my current location in MyAdapter and computed the distance using the LatLng I got from getJplatitude() and getJplongitude() from JobSeekerClass(). It is actually Job Provider class. I mistakenly named it as JobSeekerClass. I have calculated distance using distUtils and now, I want to sort my recyclerView based on the computed distance. How can I do that?

0 Answers0