How to correctly close cursor in Kotlin after use. I know how to do it in Java but doesn't matter what I do in Kotlin, it still gives warning to close it.
I tried:
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()
}
}
and the modern way:
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))
}
}
use
documentation. – Lollygag