I have an Android activity with eight TextViews, named tvStat1, tvStat2, …, tvStat8. I have a function that takes an integer as a parameter. What I want to do is something like this:
public void incrementScore(int StatisticCategory){
String s "R.id.tvStat" + String.ValueOf(StatisticCategory);
TextView tvGeneric = (TextView)findViewById(s);
// ... do something with the text in the generic TextView...
}
But of course, this doesn’t work, since the findViewById method only takes an integer as a parameter, and as such doesn’t like my way of identifying a generic TextView based on an incoming parameter. Since I only have eight TextViews, it isn’t too much effort to write a switch statement… but I think there’s got to be a better way. Any ideas?
You can use
ViewGroup.getChildCount()andViewGroup.getChildAt(). Here is an exmple.Assuming you have layout:
You can use next code to assign text to
TextViews:}