GridView只在单击项目时删除第一个项目。 基于Java>Kotlin

我有一个GridView填充TextViews。 我希望这样,当用户点击一个TextView时,将从TextView中删除TextView,并且从该ArrayList中删除该TextView中显示的字符串。 这原本在Java中工作,但转换到Kotlin时,它停止工作,现在只删除第一个项目,无论点击。

这是Kotlin代码:

grid_view!!.onItemClickListener = AdapterView.OnItemClickListener { _, _, _, _ -> val selectedItem = (tag_name as TextView).text.toString() itemList.indices.forEach { Log.d("Update", "Removing: " + selectedItem) itemList.removeAll { it == selectedItem } } } 

我想我已经在你的上一个问题中回答了。 如果你不知道该怎么做,我很乐意帮你解决问题。 首先,我不是一个android开发人员,但我认为你需要从OnItemClickListener获得position ,并且不需要for循环来做这样的事情,例如:

 grid_view!!.onItemClickListener = AdapterView.OnItemClickListener { _, _, pos, _ -> itemList.remove(pos) //by convention in android you should operate view on an adapter rather than UI // v your_grid_view_adapter.run{ remove(pos) notifyDataSetChanged() } }