How to avoid setting inline onClickListner in getView()

丶灬走出姿态 提交于 2019-12-06 11:30:58

Your activitiy has to implement the OnClickListener, and move the implemented method up to the activity level.

Based on the view parameter of the method you are able to detect from which UI object the event came from (button, textview).

As for how to detect for which record/row in a listview. You have to set a custom tag on the buttons and textviews in the getView method, that you will read in the event via getTag this tag can be a custom object too if string is not enough. Probably/recommended way is to be the position in the adapter.

I am afraid that Pentium10 answer is not correct. If the checkbox is to be clickable and checkable independently from the list item itself, (say it is defined in the list item xml layout), then it will intercept the click event, and the list item (the list's OnItemClickListener) will not receive the click at all. Therefore you must implement the onClickListener in the adapter itself - either in getView(), or have the adapter implement onClickListener - in this case the items must be tagged for the listener to know which item it is operating on.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!