I don't think you should use the ids for this as that is not what its for. I think that you should use the tag
property instead, and you will need a click listener for when the button is clicked.
private void createLayoutDynamically() {
int a = 10;
for ( int q = 1; q < a; q++) {
myButton = new Button(this);
myButton.setText("q:"+q);
myButton.setId(q);
myButton.setTag(q);
myButton.setOnClickListener(listener);
TableRow layout = (TableRow) findViewById(R.id.tableRow1);
layout.addView(myButton);
}
}
Declare a variable at the class level:
private int total;
And then when you click on the button:
Private OnClickListener listener = new OnClickListener()
{
public void onClick(View v)
{
int buttonValue = (int)v.getTag();
total += buttonValue;
myTextView.setText(String.valueOf(total));
}
}