Home > front end >  Android Formatting String in Extension Function
Android Formatting String in Extension Function

Time:03-29

I am trying to get Formatting String in AndroidViewModel by using following extension function but failed:

<string name="version_text">Version %1$s</string>

val versionText = getString(R.string.version_text, "1.0.0")

fun AndroidViewModel.getString(resId: Int, vararg formatArgs: Any) = (getApplication() as Context).getString(resId, formatArgs)

Result:

Version [Ljava.lang.Object;@bad21e0

CodePudding user response:

vararg parameters are Array types. Use the * operator to expand them:

(getApplication() as Context).getString(resId, *formatArgs)
  • Related