英文:
How to show distance between two locations in recyclerview items?
问题
我有两个类 User
和 School
,如下所示:
User: name, age, latitude, longitude
School: name, latitude, longitude, level, address, rating
我有一个用于从服务器获取的学校列表的适配器。一切都正常,但我不知道如何在每个RecyclerView项中显示用户到学校的距离。因为适配器是学校的列表,在适配器内部,我无法访问当前用户的数据。(我可以吗?)
我尝试从片段中访问RecyclerView项模型的距离TextView,但找不到任何相关的参考。
英文:
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.
答案1
得分: 1
你可以将User
实例的引用传递给你的RecyclerView.Adapter
派生类。然后,在onBindViewHolder
重写内部,你将能够访问到User
和School
实例,从而可以计算距离。
要计算距离,你可以使用:Location.distanceBetween https://developer.android.com/reference/android/location/Location#distanceBetween(double,%20double,%20double,%20double,%20float[])
英文:
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[])
答案2
得分: 0
你可以创建一个独立的CombinedData模型,你可以使用for循环对地址进行评估,必须有ID来创建新的类。
英文:
You can create a separate model of CombinedData, you can evaluate address using for loop against the ids, you must have ids to create the new class
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论