ViewModelProviders在我的片段中不起作用 [英] ViewModelProviders is not working inside my Fragment

查看:210
本文介绍了ViewModelProviders在我的片段中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的.

  • 在片段内设置对象的ArrayList
  • FragmentActivity中的观察者获取该数组 容器(托管所有片段的活动)
  • Set an ArrayList of object inside a Fragment
  • Get that array from an observer within the FragmentActivity container (the activity that hosts all the fragments)

所以,我要做的是以下事情.

So, What I have done is the following.

首先,我在其中设置SharedViewModel并从中获取数据:

First I created the SharedViewModel from where I will set and get the data from :

class SharedViewModel: ViewModel() {

    var personArrayObj:MutableLiveData<ArrayList<Person>> = MutableLiveData()

    fun setPersonArray(personArray:ArrayList<Person>){
        personArrayObj.value = personArray
    }

    val getPersonArray:LiveData<ArrayList<Person>>
    get() = personArrayObj

}

现在,我在托管所有片段创建的MainActivity内部实例化该ViewModel,因此,无论片段集位于何处,我都可以获取ArrayData:

Now , I instantiate this ViewModel inside the MainActivity that hosts all the fragments creation, so , I can get the ArrayData from wherever Fragment its set :

class MainActivity: FragmentActivity(){

private lateinit var viewModel:SharedViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        viewModel = ViewModelProviders.of(this).get(SharedViewModel::class.java)
        viewModel.getPersonArray.observe(this, Observer { it:ArrayList<Person>
            Log.d("Array:",""+it)
        })
    }

}

然后,我要做的最后一件事是从我想要的任何片段中设置该Array的值,然后,如果该值已更改,则将触发主机Activity,因此,为此,我简单地执行viewModel.setValue(personArray)放在我想要的片段上.

Then the last thing I need to do is to set the value of that Array from whatever Fragment I want, and then the host Activity will be triggered if the value has changed, so, to do this, I simple do a viewModel.setValue(personArray) at my desired Fragment .

当我尝试在任何片段中实例化SharedViewModel时,都会出现此问题:

When I try to instantiate the SharedViewModel inside any fragment I get this problem :

我试图断言!对于null,也可以使用let,但它不起作用,我想知道从何而来FragmentActivity是必需的

I tried to assert !! for the null, use let too, but is not working and I wonder from where it comes that FragmentActivity required

如果我使用viewLifecycleOwner而不是activity,它对我说,无法使用 Fragment FragmentActivity 调用它?

If I use viewLifecycleOwner instead of activity it says to me that it cant be called either with Fragment or FragmentActivity ?

有任何线索吗?

谢谢.

推荐答案

片段提供了一种辅助方法来检索非null活动-requireActivity().用它代替activity:

Fragments offer a helper method for retrieving a non-null Activity - requireActivity(). Use that instead of activity:

viewModel = ViewModelProviders.of(requireActivity()).get(SharedViewModel::class.java)

或者,您可以在应用程序中包含fragment-ktx依赖项,并使用

As an alternative, you can include the fragment-ktx dependency in your app and use the by activityViewModels() Kotlin property extension instead of using ViewModelProviders.of() at all:

val viewModel: SharedViewModel by activityViewModels()

这篇关于ViewModelProviders在我的片段中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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