I know that the base different is that setId() takes int as a parameter, while setTag() takes Object. I am asking more about practical advice.
When I generate more same elements programmatically, I set their IDs via setId(++counter) and that is fine for me to know which element has sent the onClick event. And I could do the same thing via setTag(++counter), but I am used to the previous approach.
So when should I use setTag() and am I making a mistake when using setId()?
I guess if my way is right, then I’d use setTag() when I want to pass additional data, but I’ll leave you comment on this issue first.
Your guess is right. You should use the
setId()when you only want a way to identify that particularViewin your code andsetTagwhen you want to pass additional information along with thatView(that additional data may or may not uniquely identify thatView).You could use only the
setTagmethod and pass a compound object that containsid+ additional data but in this situation you are required to build a special object instead of the more simply way, calling the two methods in question.Edit :
The docs for the
Viewclass also contain valuable information about those two methods:IDs
Tags