Apps How to set 10 textviews at once?


Hey all,

I've been trying to figure this out on my own but I'm new to Android and I can't find the answer.

I have an app with 10+ textviews and I want to create a simple loop to set the text of each one. Each textview is named sequentially "storyTitle1", "storyTitle2", "storyTitle3", etc. And the string I'm putting in each textview has already been set and can be called using getTitle(1), getTitle(2), getTitle(3), etc.

My question is, how can I setText using a variable object name?

for (int i = 10; i >= 1; i--) {
currentObject = "storyTitle" + i; // current object now equals "storyTitle1"
storyTitle1.setText(parsedDataSet.getTitle(i)); //works, but I want storyTitle1 to be the variable currentObject

Many thanks for your help. :)


You should be able to get the TextViews by id, passing them a string. If you assign them a series of incremental ids, you can loop and get them, or you can actually get all of the views that have IDs that match a certain pattern as an array using getById and getAllById, I believe. Unfortunately I am not at my dev computer at the moment.


Ok, now that I am at my dev computer, I can say that the function is findViewById, where you can specify a string and be returned the appropriate view.