因此,这是我第一次在Android中使用架构组件。我正在尝试创建一个ViewModel,它将继续返回UI元素可以使用的最新位置。我已经创建了一个如下的viewModel:
class LocationViewModel(application: Application) : AndroidViewModel(application) {
val currentLocation = MutableLiveData<Location?>()
init {
val ctx = getApplication<Application>().applicationContext
val fusedLocationProvider = LocationServices.getFusedLocationProviderClient(ctx)
val locationRequest = LocationRequest.create()
locationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
locationRequest.interval = 5000
locationRequest.fastestInterval = 2000
val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
val client = LocationServices.getSettingsClient(ctx)
client.checkLocationSettings(builder.build()).addOnFailureListener {
currentLocation.postValue(null)
}
val locationCallback = object : LocationCallback() {
override fun onLocationResult(p0: LocationResult?) {
super.onLocationResult(p0)
p0 ?: return
currentLocation.postValue(p0.lastLocation)
}
}
fusedLocationProvider.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper()
)
}
}
我在一个活动中观察了这个ViewModel,
class MainActivity : AppCompatActivity() {
private lateinit var locationText: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
locationText = findViewById(R.id.locationText)
val location = ViewModelProviders.of(this)[LocationViewModel::class.java]
location.currentLocation.observe(this, Observer { resutlLocation: Location? ->
locationText.text =
if (resutlLocation != null) "Lat: ${resutlLocation.latitude} Long: ${resutlLocation.longitude}" else "Null"
})
}
}
TextView甚至不会更新一次。应该如何做这些事情?我究竟做错了什么?
答案 0 :(得分:0)
在View中,像这样在View模型中创建函数。
fun initdata() {
val ctx = getApplication<Application>().applicationContext
val fusedLocationProvider = LocationServices.getFusedLocationProviderClient(ctx)
val locationRequest = LocationRequest.create()
locationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
locationRequest.interval = 5000
locationRequest.fastestInterval = 2000
val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
val client = LocationServices.getSettingsClient(ctx)
client.checkLocationSettings(builder.build()).addOnFailureListener {
currentLocation.postValue(null)
}
val locationCallback = object : LocationCallback() {
override fun onLocationResult(p0: LocationResult?) {
super.onLocationResult(p0)
p0 ?: return
currentLocation.postValue(p0.lastLocation)
}
}
fusedLocationProvider.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper()
)
}
然后像这样使用
val viewModel = ViewModelProviders.of(this)[LocationViewModel::class.java]
viewModel.initdata()
编辑1
您可以像这样懒惰初始化
private val users:MutableLiveData<Location?> by lazy {
MutableLiveData().also {
initdata()
}
}
更多详细信息,请参见ViewModel