Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

582
Visualizações
How do I get context inside a companion object that is inside a data class Kotlin?

I'm trying to use a resource string inside a companion object that is inside a data class. but I don't Know how to obtain context in that case in Kotlin. Anyone knows how to do it?

data class PhoneCall(
val type: String,
    val code: String,
    val description: String){
companion object{
const val SOLUTION_NO_SOLUTION = "NO_SOLUTION" 
        const val SOLUTION_TOMORROW = "71_INAT"
        const val SOLUTION_TODAY = "72_INAT"

val solutions = listOf(
PhoneCall(Service.Traffic.PICK_UP, SOLUTION_NO_SOLUTION, Resources.getSystem().getString(R.string.makeService))
)
}

I need to use a resource string in the 3 parameter, but I'm not able to get the context.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can modify you PhoneCall model to store a string resource id instead of the actual string.

data class PhoneCall(
    val type: String,
    val code: String,
    @StringRes val description: Int
) {
    companion object {
        const val SOLUTION_NO_SOLUTION = "NO_SOLUTION" 
        const val SOLUTION_TOMORROW = "71_INAT"
        const val SOLUTION_TODAY = "72_INAT"

        val solutions = listOf(
            PhoneCall(Service.Traffic.PICK_UP, SOLUTION_NO_SOLUTION, R.string.makeService)
        )
    }
}

Then, when you need to display this data in the UI (say a TextView), you can fetch the string from the resource id.

descriptionTextView.text = getString(phoneCall.description)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda