user1356791
user1356791

Reputation: 165

Dynamically changing TextViews with a for loop

I am trying to change the values of several TextView elements but iterating through an array list and adding these values. However I can't seem to find a way to change the R.id value that is used each time. For example:

for (int i=0; i<arrayList.size(); i++)
{
    TextView t = (TextView) dialog.findViewById(R.id.value+(i));
    t.setText(arrayList.get(i));
}

Where the values are in the format animals_eng1,animals_eng2 etc.. Any help appreciated.

Upvotes: 0

Views: 4260

Answers (4)

android developer
android developer

Reputation: 116392

if all of your views are on the same container (and only them) , simply iterate over its children .

if not , you can add an array of ids (in res) and iterate over it .

Upvotes: 0

Sam Dozor
Sam Dozor

Reputation: 40734

I don't know of a way to do exactly what you're asking, but here are two alternatives:

1.Create an array of Integers, and assign each element of the array to a different view id value

int[] ids = new int[arrayList.size()];
ids[0] = R.id.view0;
ids[1] = R.id.view1;
ids[2] = R.id.view2;
//...ids[n] = R.id.viewN; where n goes up to arrayList.size()
for (int i : ids){
     ((TextView)dialog.findViewById(ids[i])).setText(arrayList.get(i));
}

Note that the above method sorta defeats the point because you have to have a line for every TextView, if you want something more dynamic:

2.Tag your TextViews in your layout xml by adding android:tag="prefix0", for example, to each of your TextViews. Before the loop find the parent View of your layout, and then use the findViewWithTag method of that view within the for loop. From your code I'm guessing you're using a Dialog with a custom layout xml, so you'd first find the parent of that:

ViewGroup parent = dialog.findViewById(R.id.parent); //or whatever the name of your parent LinearLayout/RelativeLayout/whatever is
String commonPrefix = "prefix"; //or whatever you've tagged your views with
for (int i=0; i<arrayList.size(); i++){ 
    TextView t = (TextView) parent.findViewWithTag(commonPrefix+i);
    t.setText(arrayList.get(i)); 
}

Upvotes: 1

dymmeh
dymmeh

Reputation: 22306

Your best bet is to create an array containing the resource IDs of each text view and looping through them..

ex.

int[] textViewIds = new int[] { R.id.animals_eng1, R.id.animals_eng2, R.id.animals_eng3 }; 

Then in your activity you can loop through them and set the values like you desire

for (int i=0; i<arrayList.size(); i++)
{
   ((TextView)findViewById(textViewIds[i])).setText(arrayList.get(i));
}

You'll have to make sure your arrayList size is the same as the number of textview resource IDs you set or you'll end up with an out of bounds exception when looping

Upvotes: 4

MattDavis
MattDavis

Reputation: 5178

One way to solve this would be putting your resource ids in an int array and getting the resource at index i.

TextView t = (TextView) dialog.findViewById(R.id.value+(i))

becomes

TextView t = (TextView) dialog.findViewById(resourceArray[i])

Upvotes: 0

Related Questions