Abdul
Abdul

Reputation: 887

Android View Model binding in kotlin

i am facing a strange issue. I am binding a viewModel to my layout by factory design patter. When ever i build that there is difference between inferred and expected view model.

My View Model Class

class SleepTrackerViewModel(
        val database: SleepDatabaseDao,
        application: Application) : AndroidViewModel(application) {
    private var viewModelJob = Job()
    private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
    var alarmList = database.getAllAlarms()


    override fun onCleared() {
        super.onCleared()
        viewModelJob.cancel()
    }

    fun onStartTracking() {
        uiScope.launch {
            val newNight = AlarmModel()
            insert(newNight)
        }
    }

    private suspend fun insert(night: AlarmModel) {
        withContext(Dispatchers.IO) {
            database.insert(night)
        }
    }
}

So when every i try to assign the view model in fragment, it shows error that expected and inferred view model are different

My XML file

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>

        <variable
            name="viewModel"
            type="com.example.android.trackmysleepquality.Fragments.SleepTrackerViewModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- TODO: Update blank fragment layout -->
        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:listData="@{viewModel.alarmList}" />

    </LinearLayout>
</layout>

My Factory Class

class SleepTrackerViewModelFactory(
        private val dataSource: SleepDatabaseDao,
        private val application: Application) : ViewModelProvider.Factory {
    @Suppress("unchecked_cast")
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(SleepTrackerViewModel::class.java)) {
            return SleepTrackerViewModel(dataSource, application) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

Upvotes: 0

Views: 630

Answers (1)

Auxano Services
Auxano Services

Reputation: 84

Remove ViewModel from your XML file and try this one.

class AddMovieFragment : Fragment() {

  private lateinit var viewModel: AddMovieViewModel

  companion object {
    const val SEARCH_QUERY = "SEARCH_QUERY"
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    viewModel = ViewModelProviders.of(this).get(AddMovieViewModel::class.java)
  }

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, 
savedInstanceState: Bundle?): View {
    return inflater.inflate(R.layout.fragment_add_movie, container, false)
  }

Upvotes: 1

Related Questions