In my Android application, is there a way I can set the text to a textview by using a variable as part of the id?
I am trying to do something like this:
for (int i = 1; i < 6; i++){
views.setTextViewText(R.id.textView+i, "" + realtimeData.get(i).id);
}
I do have the TextViews declared in the layout xml as textView1, textView2, etc... and can access them with the static name. My issue is I do not know how many objects will be in my list. I do not want to display more than 5, but if there are fewer than 5 its ok for the TextView value to be left blank.
You're looking for the getIdentifier()
method:
for (int i = 1; i < 6; i++){
views.setTextViewText(getResources().getIdentifier("textView" + i, "id", getPackageName()), "" + realtimeData.get(i).id);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With