ViewModel不随着数据更改而更新

问题描述 投票:0回答:1

所以,这是我第一次在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甚至不会更新一次。应该如何做这些事情?我究竟做错了什么?

所以,这是我第一次在Android中使用架构组件。我正在尝试创建一个ViewModel,它将继续返回UI元素可以使用的最新位置。我创建了一个...

android kotlin mvvm observable
1个回答
0
投票

在View中,像这样在View模型中创建函数。

© www.soinside.com 2019 - 2024. All rights reserved.