title: Android kotlin MVVM - can you call viewmodel inside recyclerview adapter?
body: <p>I have a list of objects and each object has it's own list of objects (which sometimes have a list too).</p>
  
  <p>So I have one recyclerview for the list of main objects. Inside the adapter for the main objects, I create another recyclerview adapter for the list of secondary objects. (And the nesting continues like that if I get an object that has 3 nested lists)</p>
  
  <p>I use the MVVM architecture. I receive my data from an API so I define the RxJava call (Disposable) inside the ViewModel, then create an instance of that ViewModel inside an Activity and load the data.</p>
  
  <p>Now my question is, how can I load the data for the nested recyclerview lists? A viewModel is unique to an activity, and I can't call the viewModel inside an adapter (since I use dagger and inject only works in an activity)</p>
  
  <p>Examples of data: So with these examples I would be getting a Person object, inside that object there's a list with Car objects, and inside that list another list of Submodel objects.</p>
  
  <p>Nested json object:</p>
  
  <pre><code>{
      "personId": 1,
      "personName": "Bert",
      "personAge": 19,
      "isFemale": "false",
      "cars": [
                   {
              "carId": 1,
              "personId": 1,
              "carMake": "BWM",
              "model": "E36",
              "submodels": [{
                  "submodelId": 1,
                  "carId": 1,
                  "submodelName": "335i"
              }, {
                  "submodelId": 2,
                  "carId": 1,
                  "submodelName": "330d"
              }]
          }, {
              "carId": 2,
              "personId": 1,
              "carMake": "Porsche",
              "model": "911",
              "submodels": [{
                      "submodelId": 3,
                      "carId": 2,
                      "submodelName": "Carrera 4S"
                  },
                  {
                      "submodelId": 4,
                      "carId": 2,
                      "submodelName": "GT3 RS"
                  }]
          }],
          "birthDate": "2000-06-28T00:00:00"
  
  }
  </code></pre>
  
  <p>ApiInterface: </p>
  
  <pre><code>@GET("persons/{id}")
  fun getPerson(@Path("id) id: Int: Observable&lt;Person&gt;
  </code></pre>
  
  <p>PersonRepo:</p>
  
  <pre><code>  class PersonsRepository @Inject constructor(val apiInterface: ApiInterface) {
   fun getPersons(personId: Int): Observable&lt;Person&gt;{
     return apiInterface.getPerson(personId)
   }
  }
  </code></pre>
  
  <p>PersonViewModel:</p>
  
  <pre><code>class PersonViewModel @Inject constructor(private val personRepositoy: PersonRepository) : ViewModel() {
    var personResult: MutableLiveData&lt;Person&gt; = MutableLiveData()
    var personError: MutableLiveData&lt;String&gt; = MutableLiveData()
    lateinit var personDisposable: Disposable
  
    fun personResult(): LiveData&lt;Person&gt; {
      return personResult
    }
    fun personError(): LiveData&lt;String&gt; {
      return personError
    }
  
    fun loadPerson(personId: Int) {
      personDisposable = personRepository
      .getPerson(personId)
      .subscribeOn(Schedulers.newThread())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe({
        personResult.postValue(it)
    }, {
        personError.postValue(it.message)
    })
  
    fun disposeElements(){
        if(::personDisposable.isInitialized &amp;&amp; !personDisposable.isDisposed) {
              personDisposable.dispose()
          }
      }
  }
  </code></pre>
  
  <p>The models would look like this:
  PersonModel: </p>
  
  <pre><code>data class Person (
    @Json(name = "personId")
    val personId: Int,
  
    @Json(name = "personName")
    val name: String,
  
    @Json(name = "personAge")
    val age: Int,
  
    @Json(name = "isFemale")
    val isFemale: Boolean,
  
    @Json(name = "cars")
    val cars: List&lt;Car&gt;
  
    @Json(name = "birthDate")
    val birthDate: GregorianCalendar
  
  )
  </code></pre>
  
  <p>Car: </p>
  
  <pre><code>data class Car(
    @Json(name = "carId")
    val carId: Int,
  
    @Json(name = "carMake")
    val carMake: String,
  
    @Json(name = "model")
    val model: String,
  
    @Json(name = "submodels")
    val submodels: List&lt;SubModel&gt;
  )
  </code></pre>
  
  <p>SubModel:</p>
  
  <pre><code>data class SubModel(
    @Json(name = "submodelId")
    val submodelId: Int,
  
    @Json(name = "carId")
    val carId: Int,
  
    @Json(name = "submodelName")
    val submodelName: String
  )
  </code></pre>