Android 更新数据后,是否有方法刷新baseadapter内部的自定义视图?
我在listview上显示的baseadapter中有一个自定义视图。目前,我可以更新数据库中的项目,但无法刷新视图。我想在更新后自动刷新视图。是否仍要刷新?请看一下我的代码 这是莫代尔班-Android 更新数据后,是否有方法刷新baseadapter内部的自定义视图?,android,listview,kotlin,baseadapter,Android,Listview,Kotlin,Baseadapter,我在listview上显示的baseadapter中有一个自定义视图。目前,我可以更新数据库中的项目,但无法刷新视图。我想在更新后自动刷新视图。是否仍要刷新?请看一下我的代码 这是莫代尔班- class Item_Detail { var qty:Int = 0 var id:Int = 0 constructor()} 这是DatabaseHandler- val Detail: List<Item_Detail> get() { v
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
}
val详细信息:列表
得到(){
val bb=this.writeabledatabase
val seItem=ArrayList()
val myPath=DB_PATH+REAL_数据库
val db=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN\u只读)
val selectQuery=“选择*来自交易\表,其中日期=“$rowview\日期”和位置=“$rowview\位置”
val cursor=db.rawQuery(selectQuery,null)
if(cursor.moveToFirst()){
做{
val项目=项目详细信息()
item.id=cursor.getInt(cursor.getColumnIndex(“\u id2”))
item.qty=cursor.getInt(cursor.getColumnIndex(“quantity2”))
seItem.add(项目)
}while(cursor.moveToNext())
}
db.close()
回程地震
}
这是主要的活动-
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()
}}
类详细信息:AppCompatActivity(){
内部lateinit var db:DataBaseHelper
内部变量seItem:List=ArrayList()
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_详细信息)
db=DataBaseHelper(此)
detail\u date.text=rowview\u date.toString()
detail_location.text=rowview_location.toString()
getDetail()
}
私人娱乐详情(){
seItem=db.细节
val adapter=适配器详细信息(此、seItem、此)
列表\详细信息。适配器=适配器
列表\u详细信息。无效()
}}
这是Adapterclass-
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
}
类适配器\u详细信息(
内部var活动:活动,
内部变量:列表,
val上下文:上下文
):BaseAdapter(){
内部lateinit var db:DataBaseHelper
内部var充气机:充气机
初始化{
充气器=activity.getSystemService(Context.LAYOUT\u充气器\u服务)作为LayoutFlater
}
覆盖视图(位置:Int,转换视图:View?,父视图:ViewGroup?):视图{
val行视图:视图
行视图=充气机。充气(R.layout.detail_行,空)
rowView.detail_id.text=stitem[position].id.toString()
rowView.detail\u qty.text=stitem[position].qty.toString()
rowView.btn_detail.setOnClickListener{
db=DataBaseHelper(context)//启动数据库
val builder=android.app.AlertDialog.builder(上下文)
充气器=activity.getSystemService(Context.LAYOUT\u充气器\u服务)作为LayoutFlater
val视图=充气机。充气(R.layout.edit_layout,null)
builder.setView(视图)
val对话框:android.app.AlertDialog=builder.create()
var tran_edit=view.findViewById(R.id.edt_edit)
tran_edit.setText(stitem[position].qty.toString())
dialog.show()
var btn_edt=view.findviewbyd(R.id.btn_编辑)
btn_edt.setOnClickListener{
val项目=项目(
Integer.parseInt(stitem[position].id.toString()),
Integer.parseInt(1.toString()),
tran_edit.text.toString(),
name.toString(),
记录_date.toString(),
记录_location.toString(),
主记录.toString().toInt()
)
db.updateItem(项目)
dialog.discover()的
}
}
返回行视图
}
覆盖趣味getItem(位置:Int):任意{
返回针[位置]
}
覆盖getItemId(位置:Int):长{
返回stitem[position].id!!.toLong()
}
重写fun getCount():Int{
回针尺寸
}
刷新数据时,需要调用适配器
对象
main活动
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()
}
}
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
}