title: Unable to get ViewModel instance in RecyclerView.Adapter class Kotlin
body: <p>I'm new to Kotlin and trying to create an alarm clock app. In this app I'm using LiveData and RecycleView. Right now I need to change the alarm status:</p>
  
  <p><img src="https://i.imgur.com/LzTit3P.png" alt="switcher"></p>
  
  <p>Here is my <strong>AlarmsRecyclerAdapter</strong> where i tried to create <code>.onClickListener{}</code></p>
  
  <pre><code>override fun onBindViewHolder(holder: AlarmsRecyclerAdapter.AlarmItemHolder, position: Int) {
          //mAlarmViewModel = ViewModelProviders.of( context as Fragment)[AlarmViewModel::class.java]
          if (mAlarms != null) {
              val current = mAlarms!!.get(position)
              holder.view.edit_time_button.text = current.printTime()
              holder.view.switch_alarm_enabled.isEnabled = current.enabled
  
              holder.view.switch_alarm_enabled.setOnClickListener {
                  current.enabled = !current.enabled
                 // mAlarmViewModel.insert(current)
              }
          } else {
              // Covers the case of data not being ready yet.
              holder.view.edit_time_button.text = "no timer"
          }
      }
  </code></pre>
  
  <p>I also tried to get instance of <code>ViewModel</code> in the comment line, but it just throws errors like </p>
  
  <blockquote>
    <p>java.lang.ClassCastException: android.app.Application cannot be cast to androidx.fragment.app.FragmentActivity
            at com.xxx.alarm.AlarmsRecyclerAdapter.onBindViewHolder(AlarmsRecyclerAdapter.kt:58)
            at com.xxx.alarm.AlarmsRecyclerAdapter.onBindViewHolder(AlarmsRecyclerAdapter.kt:33)</p>
  </blockquote>
  
  <p>I need to change the alarms in the database, so how can I get an instance of <code>ViewModel</code> in the adapter class? Or is there better way to manage the data changing?</p>