Home > OS >  How to call a string of multiple values ​from a ViewModel
How to call a string of multiple values ​from a ViewModel

Time:07-26

I am trying to call a string that accepts two values ​​from a viewmodel

<string name="send_message">Se envió %1$s a %2$s</string>

with the next function

private fun getString(textId: Int,args: ArrayList<Any>) =
    getApplication<Application>().applicationContext?.getString(textId, args) ?: ""

    val message = getString(R.string.mystring, arrayListOf(amount,name))

But I get this error

java.util.MissingFormatArgumentException: Format specifier '%2$s'

Does anyone know what I'm doing wrong? I would greatly appreciate it

CodePudding user response:

You can do it by spreading but, varargs are more comfortable:

private fun getString(textId: Int,varargs args:Any!) 

Besides, you should use AndroidViewModel() for access to the Context from your viewmodel, But! you'd better get this string in your fragment

  • Related