Is there a way to use getString inside a util?

71 Views Asked by At

I succeeded in getString using getSystem. But I want to know how to use local string. If you know the answer to this problem, can you help me? Thanks in advance for those of you who know the answer.

class QList {
    companion object {
        val qList = listOf(
            Resources.getSystem().getString(android.R.string.cancel),
           "AAAA", "BBBB", "CCCC"
        )

        fun getQList(): ArrayList<Question> {
            var qlist = ArrayList<Question>()

            for(num in qList.indices) {
                qlist.add(Question(num + 1, qList[num], "", ""))
            }

            return qlist
        }
    }
}
2

There are 2 best solutions below

1
TylerQITX On BEST ANSWER

getString using getSystem should work for local string, just import your project's .R to your utils class.

1
Jaydeep parmar On

You need to pass context to QList class and after getting context you can simply call below line

context.getString(R.string.txt_your_string_name)