如何在 AndroidX 中实例化 ViewModel? [英] how to instantiate ViewModel In AndroidX?

查看:33
本文介绍了如何在 AndroidX 中实例化 ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用androidx库在Activity中初始化ViewModel

I want to initialize ViewModel in Activity using androidx library

我已经尝试过文档中所说的,但它不起作用..of"未解析.

I have tried what documentation says but it is not working. the ".of" is not resolved.

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.example.myapplication.databinding.ActivityMainBinding`

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityMainBinding = DataBindingUtil.setContentView(
            this, R.layout.activity_main)
        binding.setLifecycleOwner(this)

        var model = ViewModelProvider.of(this).get(SheduleViewModel::class.java)

    }
}

of 没有解决,也许androidx还有别的方法可以解决

of is not resolved, maybe there are another way to do it in androidx

推荐答案

更新答案:

事情发生了一些变化,因为以前需要的依赖项 - ViewModelProviders - 已弃用(有关详细信息,请参阅旧答案).您现在可以直接使用 ViewModelProvider 构造函数.

Things changed a little bit, as the previously needed dependency - ViewModelProviders - got deprecated (see the old answer for details). You can now use the ViewModelProvider constructor directly.

所以,在这种情况下,答案是:

So, in this case, the answer would be:

private val viewModel = ViewModelProvider(this).get(SheduleViewModel::class.java)

但是请注意,如果您包含 androidx.activity:activity-ktx:$Version 依赖项(一些常用的 AndroidX 依赖项已经为您包含了),则可以使用财产委托:

Note that, however, if you include the androidx.activity:activity-ktx:$Version dependency (a few of the commonly used AndroidX dependencies already include it for you), you can make use of property delegation:

private val viewModel: SheduleViewModel by viewModels()

在内部将使用 ViewModelProvider 并将您的 ViewModel 范围限定到您的 Activity.这只是编写相同内容的更简洁的方式.您可以通过包含 androidx.fragment:fragment-ktx:$Version 依赖项来对 Fragment 执行相同的操作(同样,其他 AndroidX 依赖项通常已经包含在内).

Which internally will use ViewModelProvider and scope your ViewModel to your Activity. It's just a more concise way of writing the same thing. You can do the same for a Fragment by including the androidx.fragment:fragment-ktx:$Version dependency instead (again, commonly already included by other AndroidX dependencies).

ViewModelProvider 构造函数和 by viewModels() 也都接受工厂作为参数(用于注入你的 ViewModel):

Both the ViewModelProvider constructor and by viewModels() also accept a factory as a parameter (useful for injecting your ViewModel):

private val viewModel = 
    ViewModelProvider(this, viewModelFactory).get(SheduleViewModel::class.java)

private val viewModel: SheduleViewModel by viewModels { viewModelFactory }

使用最适合您的那个.

旧答案:

添加 androidx.lifecycle:lifecycle-extensions:$lifecycleExtensionsVersion 依赖项以导入 ViewModelProviders.

Add the androidx.lifecycle:lifecycle-extensions:$lifecycleExtensionsVersion dependency in order to import ViewModelProviders.

这篇关于如何在 AndroidX 中实例化 ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆