Reputation: 185
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
Upvotes: 2
Views: 814
Reputation: 1545
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
}
}
Upvotes: 0