Android Kotlin 仅从 sqlite 列中获取数据并填写 autocompleteTextView

Android Kotlin Take only the data from a sqlite column and fill in an autocompleteTextView

提问人:Max Hotta 提问时间:9/16/2023 更新时间:9/16/2023 访问量:16

问:

我正在从 sqlite 中的表中获取数据并尝试将其填充到 autoCompleteTextView 中。 数据库运行良好,查询显然也是如此。 但是,在传递从查询返回的数据时,我无法仅将数据传递到适配器,而没有列名。

例如:

返回错误:CatDespModel(category = Internet)

正确返回:互联网

这是我的两个函数,它们从数据库中获取数据并填充适配器

    fun readCatDesp():ArrayList<CatDespModel>{
        val databaseHandler: DatabaseHandler = DatabaseHandler(requireContext())
        val listaGeral: ArrayList<CatDespModel> =databaseHandler.readCatDesp()
        return listaGeral
    }

    fun iniDropDown() {
        val accateg:AutoCompleteTextView = requireView().findViewById(R.id.ac_DespAdd_Cat)
        val adapter = ArrayAdapter<CatDespModel>(requireContext(),
         android.R.layout.simple_expandable_list_item_1, readCatDesp())
        accateg.setAdapter(adapter)
    }

这是访问数据库的命令

    fun readCatDesp ():ArrayList<CatDespModel>{
        val despList:ArrayList<CatDespModel> = ArrayList<CatDespModel>()
        val selectQuery = "SELECT * FROM $TABLE_DESP"
        val db = this.readableDatabase
        var cursor:Cursor? = null

        try{
            cursor = db.rawQuery(selectQuery,null)

        }catch (e:SQLiteException){
            db.execSQL(selectQuery)
            return ArrayList()
        }
        var id:Int
        var categoria:String
        if(cursor.moveToFirst()){
            do{
                id = cursor.getInt(cursor.getColumnIndex("id"))
                categoria = cursor.getString(cursor.getColumnIndex("categoria"))
                val despModel = CatDespModel(
                    categoria = categoria)
                despList.add(despModel)
            }while(cursor.moveToNext())
        }
        return despList
    }

这是我的模型:

data class CatDespModel (
    val categoria:String


)

谢谢大家

Android SQLite Kotlin 适配器

评论


答: 暂无答案