private static void exchange(int x, int y) { int freePos = DIMENSION * freeY + freeX; int myPos = DIMENSION * y + x; CharSequence text = ((Button) myAdapter.getItem(freePos)).getText(); ((Button) myAdapter.getItem(freePos)).setText(((Button) myAdapter.getItem(myPos)).getText()); ((Button) myAdapter.getItem(myPos)).setText(text); freeX = x; freeY = y; }
@Override public void onClick(View v) { if ((Math.abs(freeX - myX) == 1 && freeY == myY) || (Math.abs(freeY - myY) == 1 && freeX == myX)) { exchange(myX, myY); myActivity .getResult() .setText(String.valueOf(Integer.parseInt((String) myActivity.getResult().getText()) + 1)); if (freeY == DIMENSION - 1 && freeX == DIMENSION - 1) { boolean isSorted = true; for (int i = 0; i < DIMENSION * DIMENSION - 1; ++i) { if (!((Button) myAdapter.getItem(i)).getText().equals(String.valueOf(i + 1))) { isSorted = false; break; } } if (isSorted) { Intent intent = new Intent(myActivity, GiveName.class); myActivity.startActivityForResult(intent, 1); } } } }
/** {@inheritDoc} */ public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); mAdapter.getItem(which).onPress(); }