首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从内部自定义适配器删除数据库中的数据后重新加载ListView (Kotlin)

从内部自定义适配器删除数据库中的数据后重新加载ListView (Kotlin)
EN

Stack Overflow用户
提问于 2022-11-08 20:42:51
回答 1查看 37关注 0票数 0

目前,我有一个ListView,它使用ListView和自定义适配器从数据库中显示一组项。我还加入了一个删除按钮,它将删除它所点击的项目。所有这些都很好,但是我需要重新启动应用程序,以查看现在包含数据库中的新数据列表的新列表视图。我该怎么解决这个问题?

自定义适配器:

代码语言:javascript
运行
复制
package com.myurl.myapplication

import android.content.Context
import android.content.Intent
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteDatabase.openOrCreateDatabase
import android.database.sqlite.SQLiteStatement
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.core.content.ContextCompat.startActivity
import com.myurl.myapplication.R.id.itemDelete


class SQLiteListAdapter(
    context2: MainActivity,
    id: ArrayList<String>,
    name: ArrayList<String>,
    age: ArrayList<String>,
    height: ArrayList<String>
) : BaseAdapter() {
    var context: Context
    var ItemID: ArrayList<String>
    var ItemName: ArrayList<String>
    var ItemAge: ArrayList<String>
    var ItemHeight: ArrayList<String>

    override fun getCount(): Int {
        // TODO Auto-generated method stub
        return ItemName.size
    }

    override fun getItem(position: Int): Any? {
        // TODO Auto-generated method stub
        return null
    }

    override fun getItemId(position: Int): Long {
        // TODO Auto-generated method stub
        return 0
    }

    override fun getView(position: Int, child: View?, parent: ViewGroup?): View? {
        var child: View? = child
        val holder: Holder
        val layoutInflater: LayoutInflater
        if (child == null) {
            layoutInflater =
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            child = layoutInflater.inflate(R.layout.activity_item_body, null)

            // This is our delete button on each item. If the user clicks the delete button
            // we go ahead and delete it from the database
            val deleteImageView: ImageButton = child.findViewById(itemDelete) as ImageButton
            deleteImageView.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    var myDB: SQLiteDatabase? = null

                    myDB = openOrCreateDatabase(context.getDatabasePath("Database"), null)

                    val sql = "DELETE FROM table_name WHERE item_id = ?"
                    val statement: SQLiteStatement = myDB.compileStatement(sql)

                    statement.bindString(1, ItemID[position])
                    statement.executeUpdateDelete()
                    myDB.close()
                }
            })

            holder = Holder()
            holder.textviewname = child.findViewById(R.id.itemBodyText1)
            holder.textviewage = child.findViewById(R.id.itemBodyText2)
            holder.textviewheight = child.findViewById(R.id.itemBodyText3)
            child.setTag(holder)
        } else {
            holder = child.getTag() as Holder
        }
        holder.textviewname!!.text = ItemName[position]
        holder.textviewage!!.text = ItemAge[position]
        holder.textviewheight!!.text = ItemHeight[position]
        return child
    }

    inner class Holder {
        var textviewname: TextView? = null
        var textviewage: TextView? = null
        var textviewheight: TextView? = null
    }

    init {
        context = context2
        ItemID = id
        ItemName = name
        ItemAge = age
        ItemHeight = height
    }
}

关闭数据库连接后,我希望从自定义适配器重新加载位于MainActivity中的MainActivity。

请注意,我不是从MainActivity中删除适配器中的一个项,而是在适配器中进行删除,那么我如何刷新MainActivity视图,使其重新加载数据库中的新数据?

EN

Stack Overflow用户

回答已采纳

发布于 2022-11-09 14:02:09

在适配器中创建一个删除和刷新函数,如下所示:

代码语言:javascript
运行
复制
    fun deleteFromAllListsAndRefresh(position: Int) {
        ItemID.removeAt(position)
        ItemName.removeAt(position)
        ItemAge.removeAt(position)
        ItemHeight.removeAt(position)
        notifyDataSetChanged()
    }

并在删除过程之后调用此函数:

代码语言:javascript
运行
复制
myDB.close()
deleteFromAllListsAndRefresh(position)

我们这样做是因为您的适配器不知道您是否从db中删除了某些内容。我们将手动从列表中删除它并刷新适配器。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74366949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档