0

I'm trying to create a simple application in which there are 30 buttons and I need to initialize their text field. I created this array of buttons:

Button[][] buttons_arr = new Button[10][3];

To change each button's text I did :

for(i=0..9)  //psaudo
  for (j=0..29) //psaudo
    buttons_arr[i][j].setText(toString(some_int));

The last line is causing some problems. Why and what can I do to solve this issue ?

5 Answers 5

1

You are actually looping in 300 times instead of 30 times

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

Comments

1

try like this

for(i=0..9)  //psaudo
      for (j=0..2) //psaudo
        buttons_arr[i][j].setText(""+some_int);

Comments

1

Try this:

Button[][] b=new Button[10][3];
for(int i=0;i<10;i++)
{
     for(int j=0;j<3;j++)
     {
         b[i][j]=new Button(context);
         b[i][j].setText("something");
     }
}

1 Comment

without declaring "Button Buttons_arr[][] = new Button[10][3]" at the beggining ??
0

I have not tried 2D arrays. But my experience with a similar problem seems to be that buttons_arr[i][j] is still uninitialized. You need to either create a new button:

buttons_arr[1][1] = new Button();

or

buttons_arr[1][1] = (Button)findViewById(R.id.buttonAtPosition1_1);

1 Comment

I have this function : public void setRandomValuesToButtons() { for( i=0;i<Rows; i++){ for( j=0;j<Columns; j++){ if (i==2 && j==1){ continue; } // Random rand = new Random(); rand global variable rand_int = rand.nextInt(8); buttonsTable[i][j]=new Button(this); buttonsTable[i][j].setText(Integer.toString(rand_int)); buttonsVals[i][j]=rand_int; } } } but my buttons doesn't change their text. Why ?
0

try this

for(int i=0;i<10;i++){
    for(int j=0;j<3;j++)
        buttons_arr[i][j].setText(your text);
}

Comments

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.