如何在RecyclerView项中显示两个位置之间的距离?

huangapple go评论68阅读模式
英文:

How to show distance between two locations in recyclerview items?

问题

我有两个类 UserSchool,如下所示:

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重写内部,你将能够访问到UserSchool实例,从而可以计算距离。

要计算距离,你可以使用: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

huangapple
  • 本文由 发表于 2023年7月11日 01:50:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定