Reputation: 324
my buttons have random integers inside. I am creating them like this in tablelayout.
private static final int a=6;
private static final int b=6;
private int[] ids = {1,2,3,4,5,6,7,8,9};
Random rand = new Random();
private void createLayoutDynamically() {
won = (TableLayout)findViewById(R.id.won);
for ( int qq = 1; qq < a; qq++) {
TableRow tableRow = new TableRow(this);
for ( int q = 1; q < b; q++) {
myButton = new Button(this);
final int number = new Random().nextInt(9);
final int rand = (ids[number]);
myButton.setText(""+rand);
myButton.setTypeface(type);
myButton.setId(rand);
myButton.setTag(rand);
}
}
I want to put this integers from my buttons to array and then how to access these numbers in an array?
Upvotes: 0
Views: 75
Reputation: 26198
what you need to do is to put all your reference to the buttons in an array, so each time you add them to the TableRow
you then add them to the array of buttons.
getting the refence of buttons:
just iterate to the array of buttons and get each number.
sample:
create a global instance of arraylist of button
private ArrayList<Button> arrayButton;
add button after each iteration
for ( int q = 1; q < b; q++) {
myButton = new Button(this);
final int number = new Random().nextInt(9);
final int rand = (ids[number]);
myButton.setText(""+rand);
myButton.setTypeface(type);
myButton.setId(rand);
myButton.setTag(rand);
arrayButton.add(myButton);
}
getting the value of buttons
iterate to all the array of button
arrayButton.get(index).getTag
Upvotes: 0
Reputation: 622
You can add the numbers to an int[]
while you are creating your buttons.
int[] numberArray = new int[b];
private void createLayoutDynamically() {
...
for ( int q = 1; q < b; q++) {
...
myButton.setTag(rand);
numberArray[q] = rand;
}
}
Upvotes: 0