gpt4 book ai didi

android - 有没有办法在更新数据后刷新 baseadapter 中的自定义 View ?

转载 作者:行者123 更新时间:2023-11-30 05:02:58 24 4
gpt4 key购买 nike

我在 ListView 上显示的基本适配器内有一个自定义 View 。目前我可以更新数据库中的项目,但我无法刷新 View 。我想更新后自动刷新 View ,请问有刷新吗?请看一下我的代码。

这是模型类-

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

这是 DatabaseHandler-

  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
}

这是 MainActivity-

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

这是适配器类-

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

) : BaseAdapter() {

internal lateinit var db: DataBaseHelper
internal var inflater: LayoutInflater

init {
inflater = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
}

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

val rowView:View
rowView = inflater.inflate(R.layout.detail_row, null)
rowView.detail_id.text = stitem[position].id.toString()
rowView.detail_qty.text = stitem[position].qty.toString()


rowView.btn_detail.setOnClickListener{
db=DataBaseHelper(context)//Initiate the database
val builder=android.app.AlertDialog.Builder(context)
inflater=activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

val view=inflater.inflate(R.layout.edit_layout, null)
builder.setView(view)
val dialog: android.app.AlertDialog=builder.create()

var tran_edit=view.findViewById<EditText>(R.id.edt_edit)
tran_edit.setText(stitem[position].qty.toString())

dialog.show()

var btn_edt=view.findViewById<Button>(R.id.btn_edit)
btn_edt.setOnClickListener {
val item=Item(
Integer.parseInt(stitem[position].id.toString()),
Integer.parseInt(1.toString()),
tran_edit.text.toString(),
name.toString(),
record_date.toString(),
record_location.toString(),
master_record.toString().toInt()
)
db.updateItem(item)
dialog.dismiss()
}
}

return rowView

}

override fun getItem(position: Int): Any {
return stitem[position]
}

override fun getItemId(position: Int): Long {
return stitem[position].id!!.toLong()
}

override fun getCount(): Int {
return stitem.size
}

最佳答案

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

主 Activity

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

}

N.B. I wrote this solution using mobile so please excuse any syntax error. Let me know if it works?

附言有一个更好的适配器实现,即 CursorAdapter,它专为与数据库游标一起使用而设计。可以关注这个tutorial将其整合。

Update

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

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
}

关于android - 有没有办法在更新数据后刷新 baseadapter 中的自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57898835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com