Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextView id with a variable?

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.

like image 797
scheffetz Avatar asked Feb 18 '23 13:02

scheffetz


1 Answers

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);
}
like image 115
user Avatar answered Feb 28 '23 10:02

user