How to identify button click in getChildView of Ex

2020-04-12 08:26发布

问题:

I am building ExpandableListView in my android app. I have made child xml layout that repeats upto specific numbers. I have a Delete Button in my xml layout with id set in xml as btnDelete. By clicking delete button I want to delete specific child.

e.g:

Group:

Child 1         Delete
Child 2         Delete
Chile 3         Delete

Now my problem is that how to identify that which child delete button is clicked. As all delete buttons have same ID in my xml layout. Please suggest me a solution.

回答1:

In your adapter, where you inflate your row layout, you can set a tag (with the setTag () method) to each delete button. The tag can be the row position that this delete button corresponds to.

Later, when someone clicks the button, in the onClickListener, you get the button tag (with v.getTag ()), parse it to integer and delete the row at that position.