0

I have one spinner, I am loading data to that spinner from repository in ViewModel

  <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/spEstimateYear"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:background="@drawable/button_background"
            android:entries="@{checkmeasure.estimateFinancialyear}"
            android:padding="5dp"
            android:selectedItemPosition="@={checkmeasure.assetyearpos}" />

 <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/spAssets"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:background="@drawable/button_background"
            android:entries="@{checkmeasure.assets}"
            android:padding="5dp"
            android:selectedItemPosition="@={checkmeasure.assetpos}" />

on selection of this spinner I have to clear data in another spinner and load data to that spinner, i have cleared the ArrayList but how to clear the value set to that spinner from ViewModel, how to call notifydatachanged from ViewModel.

ViewModel class

class CheckmeasureViewModel(private val repository: UserRepository) : ViewModel() {

var estimateFinancialyear: ArrayList<FinYear> = ArrayList()
var assets: ArrayList<AssetId> = ArrayList()

init {
        estimateFinancialyear.add( FinYear,  0, select) )
        estimateFinancialyear.addAll(repository.getFinYears())    
}

fun getAssets(finYear: String?) {
assets.clear()
val assets = repository.getAsset(finYear)
}
creativecoder
  • 1,470
  • 1
  • 14
  • 23

3 Answers3

0

No, you should not call it on the ViewModel. You should use LiveData and then observe the live data from your Fragment/Activity.

// your viewmodel
private MutableLiveData<Boolean> notifyDataSetChangedLiveData = ....
// Your Ativity:
yourModel.notifyDataSetChangedLiveData.observe(this, value -> {// call notify with if/else condition});
Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86
0

How are you using the ViewModel in your Activity/Fragment? The typical usage of ViewModel is to expose LiveDatas for your Activity to observe. Pseudocode might look like

class CheckmeasureViewModel(private val repository: UserRepository) : ViewModel() {

    private val _assetsLiveData = MutableLiveData<List<Asset>>
    val assetsLiveData: LiveData<List<Asset>> = _assetsLiveData

    fun selectFinYear(finYear: FinYear) {
        val assets = repository.getAsset(finYear)
        _assetsLiveData.postValue(assets)
    }

}


//Activity
viewModel.assetsLiveData.observe(this, Observer {
    setAssetsSpinnerData(it) //This calls notifyDatasetChanged() or whatever other mechanism to update spinner
})
curioustechizen
  • 10,572
  • 10
  • 61
  • 110
0

make the bindable fields to wrap the ObservableField

var estimateFinancialyear = ObservableField<ArrayList<FinYear>>()
var assets =ObservableField< ArrayList<AssetId>>()

after getting the response for api, set the value and it will be notified automatically

fun getAssets(finYear: String?) {
  assets.set(repository.getAsset(finYear))
}
Fahad Alotaibi
  • 416
  • 3
  • 9