简体   繁体   English

如何修复 kotlin 中的字符串大小选择错误?

[英]How to fix string size selection error in kotlin?

val textoData = itemView.findViewById(R.id.texto_data_abastecimento) textoData.text = abastecimento.datam.toString().subSequence(0..10) 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:我试图仅使用前 5 个字符在适配器上显示,但最终出现以下错误:

java.lang.StringIndexOutOfBoundsException: length=0; java.lang.StringIndexOutOfBoundsException:长度=0; index=6索引=6

You can easily use the substring(0,5) extension function of Kotlin standard library.您可以轻松使用 Kotlin 标准库的substring(0,5)扩展 function。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM