I am trying to make a simple app.
I have a table with one row and seven buttons in that row.
I'm aware I don't need a 2d array, but I would like to use it.
So I can expand that later very easily.
I am using
int resID = getResources().getIdentifier("button" + i + h, "button", getPackageName());
to try and get the resource id from a Button.
My Buttons are named "button00, button01, button02 etc. (that's exactly how there are in R.java).
The numbers represent the row and column respectively.
I've seen people use this before, but for some reason I can never get it to work.
Here is my code for lights class
public class lights extends Activity implements OnClickListener {
public int numRows;
public int numCol;
public lights(int rows, int col) {
numRows = rows;
numCol = col;
}
public void createButtonListeners(Button[][] button) {
for (int i = 0; i < numRows; i++) {
for (int h = 0; h < numCol; h++) {
int resID = getResources().getIdentifier("button" + i + h, "button", getPackageName());
button[i][h] = (Button) findViewById(resID);
button[i][h].setOnClickListener((View.OnClickListener) this);
Toast.makeText(getBaseContext(), "Made listener", Toast.LENGTH_LONG).show();
}
}
}
public void reset(Array buttons[][]) {
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numCol; c++) {
//havnt got this far
}
}
}
public int wasButtonPressed() {
for (int row = 0; row < numRows; row++) {
}
return 0;
}
@Override
public void onClick(DialogInterface dialog, int which) {
}}
Code for Main_Activity
public class MainActivity extends Activity implements View.OnClickListener {
lights mlights = new lights(1, 7);
Button Button[][] = new Button[mlights.numRows][mlights.numCol];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlights.createButtonListeners(Button);
}
@Override
public void onClick(View v) {
}}
I have no errors when i type my code but it does not run.
Please correct me in any possible way