我正在尝试使用onItemSelectedListener从res / raw将文本文件显示到textview中。
我可以进行这种硬编码,但是效率不高,因为我有10多个文本文件。因此,我尝试优化代码并显示基于索引的文本文件,该索引从JSON文件读取。但是,当我尝试此操作时,textview中没有任何显示,也没有引发任何错误。它只是说找不到该文件。
spSongs.onItemSelectedListener =
object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
adapterView: AdapterView<*>,
view: View,
i: Int,
l: Long
) {
songIndex = i //remember what the user chooses
try {
var x = myJSON_array.getJSONObject(songIndex)
.getString("filename")
x = x.substring(0, x.lastIndexOf("."))
val temp = resources.getIdentifier(
x,
"raw",
packageName
)
val input_stream =
baseContext.resources.openRawResource(temp)
val text = input_stream.readBytes()
.toString(Charset.defaultCharset())
tvlyrics.text = text
} catch (e: JSONException) {
e.printStackTrace()
}
//end of try-catch for painting JSON decode
}//end of onItemSelected of Painting spinner
override fun onNothingSelected(adapterView: AdapterView<*>) {}
}// end of setOnItemSelectedListener
谢谢
答案 0 :(得分:0)
解决了这个问题,我在对象内部调用了错误的数组。