1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
ImageView[] cells;
public int getCellIndex(int x, int y)
{
return NUM_CELLS * y + x;
}
public void onCreate(...)
{
TableLayout table = (TableLayout) findViewById(R.id.my_table);
this.cells = new ImageView[NUM_ROWS*NUM_CELLS];
for (int y = 0; (y < NUM_ROWS); ++y) {
TableRow tr = new TableRow(this);
for (int x = 0; (x < NUM_CELLS); ++x) {
int idx = getCellIndex(x,y);
this.cells[idx] = new ImageView(this);
this.cells[idx].setDrawableResource(R.drawable.empty_cell);
tr.addView(this.cells[idx]);
}
table.addView(tr);
}
}
}
public void setCellColor(int x, int y, int v)
{
int idx = getCellIdx(x,y);
int resid;
switch (v) {
case RED: resid = R.drawable.red_cell; break;
case YELLOW: resid = R.drawable.yellow_cell; break;
default: resid = R.drawable.empty_cell; break;
}
this.cells[idx].setDrawableResource(resid);
} |