0

question with either easy or impossible answer, dunno. I'm new and I want to create new TextView's within a loop, but I'm having problem with TextView's variable name. I need it to be unique.. Thanks.

    int i = 1;
    while (i<=10) {
        String asd = String.valueOf(i);
        TextView textView+asd = new TextView(this);
        //new textView+asd.setText("asdd");
        i++;
    }
2
  • 1
    That will run until you run out of memory. You'll never break out of the loop. Commented Jun 14, 2013 at 23:11
  • stackoverflow.com/a/2432898/1920161 - you can see if that answer helps as I believe the OP has the same question as you Commented Jun 14, 2013 at 23:11

2 Answers 2

0

You can't do it like you posted. You need to create array of TextView's. TextView textViews[] = new TextView[10]; for(int i=0; i<textViews.length; i++) { textViews[i] = new TextView(this); }

Sign up to request clarification or add additional context in comments.

Comments

0

You will need to use your layout and do something like this

final int x = 5; // # of TextViews you want
int i = 0;

final TextView[] textViews = new TextView[x];

while (i < x) {
    TextView newTextView = new TextView(this); // this needs to be the proper context
    newTextView.setText("Whatever");
    layout.addView(newTextView); // Need to bring in your layout before this
    textViews[i] = newTextView;
    i++;
}

1 Comment

textViews[x] = newTextView; if x = 5; then all newTextView's won't save as textViews[5]??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.