简体   繁体   English

Android ViewModelProvider() 参数错误

[英]Android ViewModelProvider() parameter error

I am trying to get a value from the SharedViewModel class but the ViewModelProvider() is giving a parameter error when i am passing requireActivity() although the same initilization and assignment works in my fragments.我试图从 SharedViewModel class 中获取一个值,但是当我传递 requireActivity() 时 ViewModelProvider() 给出了一个参数错误,尽管我的片段中使用了相同的初始化和分配。

It is requiring "ViewModelStoreOwner" to be passed.它要求传递“ViewModelStoreOwner”。

class CourseRepository(val app: Application) {

    private var viewModel: SharedViewModel = ViewModelProvider(requireActivity()).get(SharedViewModel::class.java)
    val courseData = MutableLiveData<List<Course>>()

    init {
        CoroutineScope(Dispatchers.IO).launch {
            callWebService()
        }
    }

    @WorkerThread
    suspend fun callWebService() {
        if (Utility.networkAvailable(app)) {
            val retrofit = Retrofit.Builder().baseUrl(WEB_SERVICE_URL).addConverterFactory(MoshiConverterFactory.create()).build()
            val service = retrofit.create(CourseService::class.java)
            val serviceData = service.getCourseData(viewModel.pathName).body() ?: emptyList()
            courseData.postValue(serviceData)
        }
    }
}

The purpose of the ViewModel here is because i am storing the Id of the selected RecyclerView item in order to send it to a server ViewModel 的目的是因为我存储了所选 RecyclerView 项目的 Id 以便将其发送到服务器

ViewModel instances are scoped to Fragments or Activities (or anything with a similar lifecycle), which is why you need to pass in a ViewModelStoreOwner to the provider to get a ViewModel from it. ViewModel 实例的范围是片段或活动(或任何具有类似生命周期的东西),这就是为什么您需要将ViewModelStoreOwner传递给提供者以从中获取 ViewModel。 The point of ViewModels is that they will exist until the store they belong to is destroyed. ViewModel 的意义在于它们将一直存在,直到它们所属的商店被销毁。

The requireActivity method doesn't work here, because you're not inside a Fragment. requireActivity方法在这里不起作用,因为您不在 Fragment 中。

Some things to consider here:这里需要考虑的一些事情:

  • Do you really need ViewModel in this use case?在这个用例中你真的需要 ViewModel 吗? Could you perhaps use just a regular class that you can create by calling its constructor?您是否可以仅使用可以通过调用其构造函数来创建的常规 class ?
  • Could you call this Repository from your ViewModel, and pass in any parameters you need from there?能从你的 ViewModel 调用这个存储库,并从那里传递你需要的任何参数吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ViewModelProvider.of 和 ViewModelProvider 在 Android Java 中均已弃用 - ViewModelProvider.of and ViewModelProvider both are deprecated in Android Java 使用模拟 ViewModel 测试 Android ViewModelProvider - Testing Android ViewModelProvider with a mock ViewModel 在 AppCompatActivity 中使用 ViewModelProvider(this) 的运行时错误 - Runtime Error using ViewModelProvider(this) in AppCompatActivity Android ViewModelProvider.of() 找不到符号 - Android ViewModelProvider.of() cannot find symbol 在 Android 中,如何在单独的文件中获取 OnItemSelectedListener 中的 viewmodelprovider? - In Android, how to get viewmodelprovider in OnItemSelectedListener in separate file? 使用Android上的Dagger和Java,ViewModelProvider.Factory在片段上保持为空 - ViewModelProvider.Factory remains null on fragment using Dagger and Java on Android ViewModelProvider() 创建一个错误,虽然自己的活动正在扩展 AppCompatActivity - ViewModelProvider() creates an error, although own activity is extending AppCompatActivity 尝试使用Dagger2和Kotlin将ViewModelProvider注入Activity时出错 - Error while trying to inject ViewModelProvider into Activity with Dagger2 and Kotlin android中的“无效参数”错误 - “invalid parameter” error in android 在 android 中使用“by viewModels()”与“ViewModelProvider(this).get(ViewModel::class.java)”查看 model 初始化 - View model initialization using “by viewModels()” vs “ViewModelProvider(this).get(ViewModel::class.java)” in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM