jeudi 31 décembre 2015

Android change value of method to check against button ID

I am trying to make a method to iterate over a set of buttons and up the count of they meet the correct condition. In doing so I want to know if it is possible to increment the value of methods such as R.id.gridButton method based on the value of the counter using regex or if that is not possible, would reflection would be the correct way to go about it.

Example: i == 1 R.id.gridButton1

i == 2 R.id.gridButton2

public int getBoardSize() {
        int buttonCount = 0;
        TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
        System.out.println("Table child count: " + tableLayout.getChildCount());

        for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) {
            View tableLayoutChild = tableLayout.getChildAt(rowIndex);
            if (tableLayoutChild instanceof TableRow) {
                for (int i = 0; i < ((ViewGroup) tableLayoutChild).getChildCount(); i++) {
                    System.out.println("Value of i: " + i);
                    View view = ((ViewGroup) tableLayoutChild).getChildAt(i);
                    if (view instanceof Button && view.getId() == R.id.gridButton1 ) {
                        buttonCount++;
                    }
                }
            }
        }
        System.out.println("Child count: " + buttonCount);
        return buttonCount;
    }





Aucun commentaire:

Enregistrer un commentaire