简体   繁体   中英

Android passing query parameter to viewmodel

I have a view and viewmodel which has 2 functionalities -

1) Clicking a button in view and getting data.

2) A Spinner where you can select an item and ask the viewmodel to get data for that item as a query parameter.

I already implemented the first point like this -

MyView code -

viewModel.onGetDataClicked.observe(this, Observer {
    //....
})

My ViewModel code -

private val viewState = MyViewState()

    val onGetDataClicked =
        Transformations.map(dataDomain.getData(MyAction.GetDataAction)) {
            when (it) {
                ....
            }
        }

MyAction code -

sealed class MyAction : Action {
    object GetDataAction : MyAction()
}

My question is how do I pass the spinner value from view to the viewmodel? Since in viewmodel I have a val onGetDataClicked and not a function.

Hi you can use the selectedItemPosition attribute from the view and pass position to viewModel, accordingly you can map the item using the position.

layout.xml

<?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 class="FeedbackBinding">

        <variable
            name="vm"
            type="com.ec.service.ServiceViewModel" />
    </data>
            <androidx.appcompat.widget.AppCompatSpinner
                android:id="@+id/unitAET"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:selectedItemPosition="@={vm.selectedUnitPosition}"
                app:entries="@{vm.unitNames}"
                app:layout_constraintEnd_toEndOf="@+id/textView10"
                app:layout_constraintStart_toStartOf="@+id/textView10"
                app:layout_constraintTop_toBottomOf="@+id/textView10" />

        </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

the selectedUnitPosition is a MutableLeveData

fragment.kt

In your fragment initialise the vm (viewModel)

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    withViewModel<ServiceViewModel>(factory) {
        binding.vm = this
    }
}

First you should get the item value in the view itself, after that pass the item value to the required method in the ViewModel and from the ViewModel to the Repository(where you are querying the data from).

    // in view
    viewModel.onGetDataClicked(item:DataType).observe(this, Observer {
        //....
    })

    //in viewmodel
    private val viewState = MyViewState()

        val onGetDataClicked:(item:DataType) =
          Transformations.map(dataDomain.getData(MyAction.GetDataAction)) {

    //you have item here, pass it where you require
                when (it) {
                    ....
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM