zee*_*han 2 android cursor kotlin
使用后如何正确关闭Kotlin中的光标。我知道如何在Java中执行此操作,但是不管我在Kotlin中执行什么操作,它仍然会警告您关闭它。
我试过了:
val cursor = context!!.getContentResolver().query(DbProvider.CONTENT_URI_VERSES, null, where, null, null)!!
if (cursor.moveToFirst()) {
try {
arabicTextTV.text = cursor.getString(cursor.getColumnIndex(DbHelper.COL_ARABIC1))
} finally {
cursor.close()
}
}
Run Code Online (Sandbox Code Playgroud)
和现代的方式:
val cursor = context!!.getContentResolver().query(DbProvider.CONTENT_URI_VERSES, null, where, null, null)!!
if (cursor.moveToFirst()) {
cursor.use {
arabicTextTV.text = cursor.getString(cursor.getColumnIndex(DbHelper.COL_ARABIC1))
}
}
Run Code Online (Sandbox Code Playgroud)
context?.contentResolver?.query(DbProvider.CONTENT_URI_VERSES, null, where, null, null)
?.use {
if (it.moveToFirst()) {
arabicTextTV.text = it.getString(it.getColumnIndex(DbHelper.COL_ARABIC1))
}
}
Run Code Online (Sandbox Code Playgroud)
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html
| 归档时间: |
|
| 查看次数: |
417 次 |
| 最近记录: |