Home > Back-end >  How to fix string size selection error in kotlin?
How to fix string size selection error in kotlin?

Time:04-12

val textoData = itemView.findViewById(R.id.texto_data_abastecimento) textoData.text = abastecimento.datam.toString().subSequence(0..10)

        val textoHora = itemView.findViewById<TextView>(R.id.texto_hora_abastecimento)
        textoHora.text = abastecimento.hodomDm.toString().subSequence(0..5)

I'm trying to use only the first 5 characters for display on the adapter and I end up getting the following error:

java.lang.StringIndexOutOfBoundsException: length=0; index=6

CodePudding user response:

You can easily use the substring(0,5) extension function of Kotlin standard library.

    textoHora.text = abastecimento.hodomDm.toString().substring(0,5)
  • Related