无法在 kotlin 中使用 hashmap 将房间数据库数据显示到列表视图 (Android)
cannot show room database data to listview using hashmap in kotlin (Android)
我借助一些在线教程在 kotlin 中创建了一个简单的房间数据库,它在日志中提供了正确的输出,但在列表视图中没有显示所需的结果
代码
var dataList = ArrayList<HashMap<String, String>>()
fun loadIntoList() {
dataList.clear()
var db = Room.databaseBuilder(applicationContext, AppDb::class.java, "BookDB").build()
val thread = Thread {
db.bookDao().getAllItems().forEach()
{
val map = HashMap<String, String>()
map.put("name", { it.itemName }.toString())
map.put("quantity", { it.quantity }.toString())
map.put("gst", { it.gst }.toString())
map.put("amount", { it.amount }.toString())
dataList.add(map)
}
findViewById<ListView>(R.id.listView).adapter =
CustomAdapter(this@MainActivity, dataList)
}
thread.start()
}
我不确定 { it.itemName }
和它的其他朋友做了什么,它在下面代码段的日志中有效
Log.i("Fetch Records", "Id: : ${it.itemId}")
Log.i("Fetch Records", "Item Name: : ${it.itemName}")
Log.i("Fetch Records", "Quantity: : ${it.quantity}")
Log.i("Fetch Records", "GST%: : ${it.gst}")
Log.i("Fetch Records", "Amount: : ${it.amount}")
这是我的适配器class
class CustomAdapter(
private val context: Context,
private val ItemDataList: ArrayList<HashMap<String, String>>
) : BaseAdapter() {
private val inflater: LayoutInflater =
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
override fun getCount(): Int {
return ItemDataList.size
}
override fun getItem(position: Int): Int {
return position
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var dataitem = ItemDataList[position]
val rowView = inflater.inflate(R.layout.list_row, parent, false)
rowView.findViewById<TextView>(R.id.row_name).text = dataitem["name"]
rowView.findViewById<TextView>(R.id.row_quantity).text = dataitem["quantity"]
rowView.findViewById<TextView>(R.id.row_gst).text = dataitem["gst"]
rowView.findViewById<TextView>(R.id.row_amount).text = dataitem["amount"]
rowView.tag = position
return rowView
}
}
输出截图
我知道 >hashmap 出了点问题,但我不知道出了什么问题(我已经正确分配了字符串名称)
将在主要 activity
中进行以下更改
val map = HashMap<String, String>()
map.put("name", it.itemName)
map.put("quantity", it.quantity)
map.put("gst",it.gst)
map.put("amount",it.amount)
dataList.add(map)
我借助一些在线教程在 kotlin 中创建了一个简单的房间数据库,它在日志中提供了正确的输出,但在列表视图中没有显示所需的结果
代码
var dataList = ArrayList<HashMap<String, String>>()
fun loadIntoList() {
dataList.clear()
var db = Room.databaseBuilder(applicationContext, AppDb::class.java, "BookDB").build()
val thread = Thread {
db.bookDao().getAllItems().forEach()
{
val map = HashMap<String, String>()
map.put("name", { it.itemName }.toString())
map.put("quantity", { it.quantity }.toString())
map.put("gst", { it.gst }.toString())
map.put("amount", { it.amount }.toString())
dataList.add(map)
}
findViewById<ListView>(R.id.listView).adapter =
CustomAdapter(this@MainActivity, dataList)
}
thread.start()
}
我不确定 { it.itemName }
和它的其他朋友做了什么,它在下面代码段的日志中有效
Log.i("Fetch Records", "Id: : ${it.itemId}")
Log.i("Fetch Records", "Item Name: : ${it.itemName}")
Log.i("Fetch Records", "Quantity: : ${it.quantity}")
Log.i("Fetch Records", "GST%: : ${it.gst}")
Log.i("Fetch Records", "Amount: : ${it.amount}")
这是我的适配器class
class CustomAdapter(
private val context: Context,
private val ItemDataList: ArrayList<HashMap<String, String>>
) : BaseAdapter() {
private val inflater: LayoutInflater =
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
override fun getCount(): Int {
return ItemDataList.size
}
override fun getItem(position: Int): Int {
return position
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var dataitem = ItemDataList[position]
val rowView = inflater.inflate(R.layout.list_row, parent, false)
rowView.findViewById<TextView>(R.id.row_name).text = dataitem["name"]
rowView.findViewById<TextView>(R.id.row_quantity).text = dataitem["quantity"]
rowView.findViewById<TextView>(R.id.row_gst).text = dataitem["gst"]
rowView.findViewById<TextView>(R.id.row_amount).text = dataitem["amount"]
rowView.tag = position
return rowView
}
}
输出截图
我知道 >hashmap 出了点问题,但我不知道出了什么问题(我已经正确分配了字符串名称)
将在主要 activity
中进行以下更改 val map = HashMap<String, String>()
map.put("name", it.itemName)
map.put("quantity", it.quantity)
map.put("gst",it.gst)
map.put("amount",it.amount)
dataList.add(map)