Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

584
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda