簡體   English   中英

Kotlin使用#close后應釋放此光標

[英]Kotlin This Cursor should be freed up after use with #close

使用后如何正確關閉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()
            }
        }

和現代的方式:

        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))
            }
        }

在此處輸入圖片說明

在此處輸入圖片說明

        context?.contentResolver?.query(DbProvider.CONTENT_URI_VERSES, null, where, null, null)
        ?.use {
            if (it.moveToFirst()) {
                arabicTextTV.text = it.getString(it.getColumnIndex(DbHelper.COL_ARABIC1))
            }
        }

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM