I have a ListView and each row contains button. I don’t want to assign to each button its own listener object, cause it will create a lot of objects. But the question is how to determine row number or contextual information when button is clicked?
Share
Every View (including Button) can be “identified” via its
setId(int)method.When clicked you will be passed a view to a
View.OnClickListener.onCLick(view). Just extract the ID and you’ll know which button this is.When creating a button:
and when this button is clicked:
Update: answer was updated based on @Octavian Damiean’s comment: for identifying Views
setId()should be used instead ofsetTag().