javaandroidandroid-recyclerviewandroid-adapter

How to show distance between two locations in recyclerview items?


I have two classes User and School as follow:

User: name, age, latitude, longitude

School: name, latitude, longitude, level, address, rating

And I have an adapter for lists of schools that comes from server. Everything is OK but I don't know how to show distance of user from school in each item of recyclerview. Because adapter is a list of Schools and inside the adapter I can't access to current user data. (Can I?)

I tried to access the distance TextView of RecyclerView Item model from fragment but couldn't find any reference for this.


Solution

  • You could pass a reference of User instance to your RecyclerView.Adapter derived class. Then inside in onBindViewHolder override you will have access to both User and School instances which will allow you to calculate distance.

    To calculate distance you can use: Location.distanceBetween https://developer.android.com/reference/android/location/Location#distanceBetween(double,%20double,%20double,%20double,%20float[])