1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to set 10 textviews at once?

Discussion in 'Android Development' started by devmob, Dec 7, 2009.

  1. devmob

    devmob Lurker
    Thread Starter

    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. :)
     



    1. Download the Forums for Android™ app!


      Download

       
  2. wige

    wige Newbie

    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.
     
  3. wige

    wige Newbie

    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.
     
Loading...
Similar Threads - set textviews once
  1. halfbeing
    Replies:
    3
    Views:
    437
  2. 06Honda
    Replies:
    7
    Views:
    569
  3. halfbeing
    Replies:
    2
    Views:
    507
  4. Jimmy the rebus dobs
    Replies:
    5
    Views:
    370
  5. Iamk_khan20
    Replies:
    0
    Views:
    276
  6. james fitzgerald
    Replies:
    5
    Views:
    798
  7. javierself
    Replies:
    0
    Views:
    392
  8. kumaranil13k
    Replies:
    8
    Views:
    780
  9. kumaranil13k
    Replies:
    2
    Views:
    597
  10. countryblue
    Replies:
    4
    Views:
    568

Share This Page

Loading...