0

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Garrett Hart
  • 7
  • 1
  • 4
  • Possible duplicate of [Android, getting resource ID from string?](http://stackoverflow.com/questions/4427608/android-getting-resource-id-from-string) – OneCricketeer Jan 22 '17 at 14:42

1 Answers1

2

If you are trying to get an id, the second argument should be "id".

You're trying to get an R.id value, not an R.button value because those don't exist

You also implemented the wrong click listener on your lights Activity (which should not extend Activity anyway, but that's unrelated to your question... Just don't be surprised if your current code doesn't work)

Hint: try this public lights(Context c, int rows, int col) and use c.getResources using that Context to create your resources, or possibly a setButtons(Button[][] buttons) method that you'd call from the Activity (since that's the only place you can call findViewById)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Sorry I did have R.id as the argument. I changed it to R.button as a possible solution – Garrett Hart Jan 22 '17 at 00:43
  • I'd suggest you move all code into the MainActivity first , but `getResources().getIdentifier("button" + i + h, "id",` is correct, though I'd be careful to make sure i and h aren't added together to a single number – OneCricketeer Jan 22 '17 at 14:41
  • Your code doesn't work because `findViewById` cannot be called from the lights class. It's not an Activity because 1) you've not called onCreate 2) there's no setContentView called, so there's no UI – OneCricketeer Jan 22 '17 at 14:44
  • OK I get it now, I'll try that. Thanks for the help – Garrett Hart Jan 22 '17 at 15:41