I created a simple room database in kotlin with the help of some online tutorials it gives correct output in the log but doesn't show the desired result in listview
the code
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()
}
I am not sure what about { it.itemName }
and its other friends do, it works in the log as of below snippet
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}")
this is my adapter 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
}
}
I know something is wrong with the >hashmap but I can't figure out what's wrong (i have assigned string names correctly)
the following changes were to be made in the main 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)