user1047069
user1047069

Reputation: 953

setting text in an array of buttons

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 ?

Upvotes: 0

Views: 110

Answers (5)

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

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");
     }
}

Upvotes: 1

Pratik
Pratik

Reputation: 30855

try this

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

Upvotes: 0

Bkteo
Bkteo

Reputation: 93

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);

Upvotes: 0

Rasel
Rasel

Reputation: 15477

try like this

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

Upvotes: 1

Rajdeep Dua
Rajdeep Dua

Reputation: 11230

You are actually looping in 300 times instead of 30 times

Upvotes: 1

Related Questions