• 安卓Cursor封装,取值带设置默认值


    目的

    有时候,使用Cursor查询数据库中某个不存在的字段时,要求能返回一个默认值。

    代码

    class CursorHelper(private var cursor: Cursor?) {
    
        init {
            if (cursor == null) {
                Log.e("", "cursor is null")
            }
        }
    
        fun swapCursor(cursor: Cursor) {
            this.cursor = cursor
        }
    
        fun getBoolean(columnName: String?, defValue: Boolean): Boolean {
            return try {
                if (cursor == null) {
                    defValue
                } else {
                    cursor!!.getInt(cursor!!.getColumnIndexOrThrow(columnName)) != 0
                }
            } catch (e: Exception) {
                e.printStackTrace()
                defValue
            }
        }
    
        fun getFloat(columnName: String?, defValue: Float): Float {
            return try {
                cursor?.getFloat(cursor!!.getColumnIndexOrThrow(columnName)) ?: defValue
            } catch (e: Exception) {
                e.printStackTrace()
                defValue
            }
        }
    
        fun getInt(columnName: String?, defValue: Int): Int {
            return try {
                cursor?.getInt(cursor!!.getColumnIndexOrThrow(columnName)) ?: defValue
            } catch (e: Exception) {
                e.printStackTrace()
                defValue
            }
        }
    
        fun getDouble(columnName: String?, defValue: Double): Double {
            return try {
                cursor?.getDouble(cursor!!.getColumnIndexOrThrow(columnName)) ?: defValue
            } catch (e: Exception) {
                e.printStackTrace()
                defValue
            }
        }
    
        fun getLong(columnName: String?, defValue: Long): Long {
            return try {
                cursor?.getLong(cursor!!.getColumnIndexOrThrow(columnName)) ?: defValue
            } catch (e: Exception) {
                e.printStackTrace()
                defValue
            }
        }
    
        fun getString(columnName: String?, defValue: String): String {
            return try {
                cursor?.getString(cursor!!.getColumnIndexOrThrow(columnName)) ?: defValue
            } catch (e: Exception) {
                e.printStackTrace()
                defValue
            }
        }
    
        fun moveToNext(): Boolean {
            return cursor != null && cursor!!.moveToNext()
        }
    
        fun moveToFirst(): Boolean {
            return cursor != null && cursor!!.moveToFirst()
        }
    
        fun getCount(): Int {
            return if (cursor == null) {
                0
            } else {
                cursor!!.count
            }
        }
    
        fun close() {
            if (cursor != null && !cursor!!.isClosed) {
                cursor!!.close()
            }
        }
    
        companion object {
            fun from(cursor: Cursor?): CursorHelper {
                return CursorHelper(cursor)
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    示例

    int limitSecond = cursorHelper.getInt(TIME_LIMIT, Integer.MAX_VALUE)
    
    • 1

    作者:帅得不敢出门

  • 相关阅读:
    跨境电商:经济合作新引擎,技术赋能新亮点
    Hive3.1.2分区与排序(内置函数)
    Abnova ABCB10(人)重组蛋白说明书
    c++ Makefile clion ide remote构建
    链接元宇宙,开启新纪元
    git的安装和基本配置以及基本命令.
    vue3.2+ts封装axios
    Python武器库开发-基础篇(三)
    线路曲线坐标计算程2
    基于SpringBoot和PotsGIS的各省地震震发可视化分析
  • 原文地址:https://blog.csdn.net/zmlovelx/article/details/125626856