困っていること
private inner class ListItemClickListener : AdapterView.OnItemClickListener {
override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
val item: MutableMap<Any,Any> = parent.getItemAtPosition(position) as MutableMap<Any, Any>
Toast.makeText(this@MainActivity, "$position", Toast.LENGTH_LONG).show()
val menuName = item["name"] as String
val menuPrice = item["price"] as Int
val set = "$menuName $menuPrice 円 "
item["people"] = 1
}
}
エラー文では Unchecked cast: Any! to MutableMap<Any, Any> となります。
目標
@Suppress("UNCHECKED_CAST")を使わずに、このエラーを解消したい
また、MutableMap<*,*>
とすると、次のitem["people"]= 1
で型指定がないためエラーが出てしまうので出来ない
(ちなみにAnyであれば大丈夫)。
これまでに試したこと
private inner class ListItemClickListener : AdapterView.OnItemClickListener {
override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
val ni :GenericTypeIndicator<Map<Any,Any>> = object : GenericTypeIndicator<Map<Any,Any>>(){}
val item: MutableMap<Any,Any> = parent.getItemAtPosition(position).getValue(ni)
Toast.makeText(this@MainActivity, "$position", Toast.LENGTH_LONG).show()
val menuName = item["name"] as String
val menuPrice = item["price"] as Int
val set = "$menuName $menuPrice 円 "
item["people"] = 1
}
}
エラー文
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch
private inner class ListItemClickListener : AdapterView.OnItemClickListener {
override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
val item: MutableMap<Any,Any> = parent.getItemAtPosition(position).filterIsInstance<MutableMap<Any,Any>>()
Toast.makeText(this@MainActivity, "$position", Toast.LENGTH_LONG).show()
val menuName = item["name"] as String
val menuPrice = item["price"] as Int
val set = "$menuName $menuPrice 円 "
item["people"] = 1
}
}
エラー文
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch
最後に
なかなかネット中を探し回っても自分にピッタリ合う解決策が見つからなかったので、どうか分かる方がいらっしゃいましたら、ご教授ください。よろしくお願いします。