Transformations not importing in Android studio

334 Views Asked by At

I am trying to add currency symbol with the cost using Transformations of Live Data object, but it is not importing.

private val _price = MutableLiveData<Double>()
val price: LiveData<String> = Transformations.map(_price) {
   NumberFormat.getCurrencyInstance().format(it)
}

Please debug

1

There are 1 best solutions below

0
On

if you are using androidx.lifecycle with 2.6.0 or newer version then you should use the following code.

Your Example

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map

private val _price = MutableLiveData<Double>()
val price: LiveData<String> = _price.map("${NumberFormat.getCurrencyInstance().format(_price)}")

My Example

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.map

class PagerViewModel : ViewModel() {
    private val _index = MutableLiveData<Int>()
    val mPosition: LiveData<Int> = _index.map{ it }
    fun setIndex(index: Int) {
        _index.value = index
    }
}