更新数据后,有什么方法可以刷新baseadapter中的自定义视图吗?

时间:2019-09-12 02:04:20

标签: android listview kotlin baseadapter

我在baseadapter中有一个自定义视图,该视图显示在listview上。目前,我可以更新数据库中的项目,但无法刷新视图。我想在更新后自动刷新视图。是否仍然要刷新?请看一下我的代码。

这是模型类-

"functions": {
    "predeploy": []
 }

这是DatabaseHandler-

class Item_Detail {
   var qty:Int = 0
   var id:Int = 0
   constructor()}

这是MainActivity-

  val Detail: List<Item_Detail>
    get() {
        val bb=this.writableDatabase
        val seItem=ArrayList<Item_Detail>()
        val myPath=DB_PATH + REAL_DATABASE
        val db=SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY)
        val selectQuery="Select * From Transaction_table Where date='$rowview_date' And location='$rowview_location'"
        val cursor=db.rawQuery(selectQuery, null)

        if (cursor.moveToFirst()) {

            do {
                val item=Item_Detail()
                item.id=cursor.getInt(cursor.getColumnIndex("_id2"))
                item.qty=cursor.getInt(cursor.getColumnIndex("quantity2"))
                seItem.add(item)
            } while(cursor.moveToNext())
        }
        db.close()
        return seItem
    }

这是Adapterclass-

class Detail : AppCompatActivity() {

internal lateinit var db: DataBaseHelper
internal var seItem: List<Item_Detail> = ArrayList<Item_Detail>()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_detail)

    db = DataBaseHelper(this)
    detail_date.text = rowview_date.toString()
    detail_location.text = rowview_location.toString()
    getDetail()


}
private fun getDetail() {
    seItem = db.Detail
    val adapter = Adapter_detail(this, seItem,this)
    list_detail.adapter=adapter
    list_detail.invalidate()
}}

1 个答案:

答案 0 :(得分:0)

刷新数据时,需要在adapter对象上调用notifyDataSetChanged()。您需要进行以下更改才能使用它,

MainActivity

class Detail : AppCompatActivity() {

    internal lateinit var db: DataBaseHelper
    internal var seItem: MutableList<Item_Detail> = ArrayList<Item_Detail>()
    internal lateinit var adapter : Adapter_detail

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail)

        db = DataBaseHelper(this)
        detail_date.text = rowview_date.toString()
        detail_location.text = rowview_location.toString()
        adapter = Adapter_detail(this, seItem,this)
        list_detail.adapter=adapter
        getDetail()
    }

    private fun getDetail() {
        adapter.refresh(db.Detail)
    }

}

适配器类

class Adapter_detail(
    internal var activity: Activity,
    internal var stitem: MutableList<Item_Detail>,
    val context:Context

    ) : BaseAdapter() {

    ...

    fun refresh(newList: List<Item_Detail>) {
         stitem.clear()
         stitem.addAll(newList)
         notifyDataSetChanged()
    }

}
  

不适用。我使用移动设备编写了此解决方案,因此请原谅任何语法错误。   让我知道它是否有效吗?

P.S。有一个更好的适配器实现,即CursorAdapter,它是专门为与数据库游标一起使用而设计的。您可以按照此tutorial进行集成。

  

更新

我忘记了一件事,您需要在refresh()函数的ClickListener回调中调用getView(),在其中执行更新操作。因此,它将如下所示,

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    ...

        btn_edt.setOnClickListener {
            val item= ...
            db.updateItem(item)
            dialog.dismiss()
            refresh(db.Detail)
        }
    }

    return rowView
}