问题描述
我正在做一个学校项目,但遇到了困难。
基本上,我需要按下一个按钮并打开一个带有与坐标关联的标记的 Google 地图。
要打开一个新的 MapActivity,我在 MainActivity 中使用它:
fun launchGoogleActivity(latitude : Double,longitude : Double,title: String): Boolean
{
val intent = Intent(this,MapsActivity::class.java).apply
{
putExtra("latitude",latitude)
putExtra("longitude",longitude)
putExtra("title",title)
}
startActivity(intent)
return true
}
在我的 MapActivity 中,我用它来放置新标记:
val extras = intent.extras
val latitude: Double
val longitude: Double
val title: String
latitude = extras?.getString("latitude")?.todouble()!!
longitude = extras.getString("longitude")?.todouble()!!
title = extras.getString("name").toString()
val newPoint = LatLng(latitude,longitude)
mMap.addMarker(MarkerOptions().position(newPoint).title(title))
mMap.moveCamera(CameraUpdateFactory.newLatLng(newPoint))
我的适配器中有这些数据:
holder.buttonMap.setonClickListener
{
val latitude = user?.location?.location?.latitude?.todouble()
val longitude = user?.location?.location?.longitude?.todouble()
val name : String = "Localisation de: " + user?.name?.nom + " " + user?.name?.prenom
}
如何在我的适配器中执行 launchGoogleActivity(latitude,longitude,name)
?
谢谢!
解决方法
您可以使用以下代码从适配器启动活动。
在您调用方法传递参数的活动中
YourAdapter(this)
在论证中考虑上下文
class CategoriesListAdapter(private val context: Context) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() { }
然后在您的 holder.buttonMap.setOnClickListener 方法中:
context.startActivity(Intent(context,SubCategoryActivity::class.java)
.putExtra(AppConstants.CATEGORY_ID,categoryData.getId())
.putExtra(AppConstants.CATEGORY_NAME,categoryData.getName()))
,
适配器中的传递函数
class YourAdapter(private val listener : (Double,Double,String) -> Unit) : RecyclerView.Adapter<RecyclerView.ViewHolder>()
并在您的 viewHoler 中
val latitude = user?.location?.location?.latitude?.toDouble()
val longitude = user?.location?.location?.longitude?.toDouble()
val name : String = "Localisation de: " + user?.name?.nom + " " + user?.name?.prenom
holder.buttonMap.setOnClickListener {
listener(latitude,longitude,name)
}
并在您设置适配器的 MainActivity 中
YourAdapter(){latitude: Double,longitude: Double,name: String -> launchMapActivity(latitude,name)}
,
将上下文添加到适配器的构造函数并在按钮上设置 onclicklistener。在其中添加此代码。
holder.buttonMap.setOnClickListener {
val intent = Intent(context,MapsActivity::class.java).apply {
putExtra("latitude",latitude)
putExtra("longitude",longitude)
putExtra("title",title)}
context.startActivity(intent)}
,
您可以将您的函数从活动调用到您的适配器
在您的适配器中,添加一个我们将在您的活动中覆盖的接口类。
class MarkerAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var callback: Callback? = null
inner class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
init {
holder.buttonMap.setOnClickListener{
callback?.onItemClick(user)
}
}
}
interface Callback {
fun onItemClick(user: User?)
}
}
然后在您的活动中,您只需要实现适配器的回调
class GoogleFragment() : MarkerAdapter.Callback {
override fun onItemClick(user: User?) {
val latitude = user?.location?.location?.latitude?.toDouble()
val longitude = user?.location?.location?.longitude?.toDouble()
val name : String = "Localisation de: " + user?.name?.nom + " " + user?.name?.prenom
}
}
添加上述代码后,从您的 Activity 中的适配器初始化回调属性。
val adapter = MarkerAdapter()
adapter.callback = this
recylerView.adapter = adapter