@Override public void onItemSelected(AdapterView<?> adapter, View view, int pos, long id) { if (modifyingSpinner) { return; } Tossup t = game.currTossup(); String playerName = (String) adapter.getSelectedItem(); // team a was selected if (adapter.getId() == R.id.teamaplayerspinner) { // team a won if (t.getWinnerTeam().equals(game.getTeamA())) { Player p = t.getWinnerTeam().getPlayer(playerName); t.setWinnerPlayer(p); } // team a lost else { Player p = t.getLoserTeam().getPlayer(playerName); t.setLoserPlayer(p); } } // team b was selected else { // team b won if (t.getWinnerTeam().equals(game.getTeamB())) { Player p = t.getWinnerTeam().getPlayer(playerName); t.setWinnerPlayer(p); } // team b lost else { Player p = t.getLoserTeam().getPlayer(playerName); t.setLoserPlayer(p); } } }
@Override public void onNothingSelected(AdapterView<?> arg0) { if (modifyingSpinner) { return; } game.currTossup().setWinnerPlayer(null); }
private void updatePlayerSpinner() { modifyingSpinner = true; Tossup t = game.currTossup(); Spinner winnerSpinner, loserSpinner; if (t.getWinnerTeam().getName().equals(teamAName)) { winnerSpinner = (Spinner) findViewById(R.id.teamaplayerspinner); loserSpinner = (Spinner) findViewById(R.id.teambplayerspinner); } else { winnerSpinner = (Spinner) findViewById(R.id.teambplayerspinner); loserSpinner = (Spinner) findViewById(R.id.teamaplayerspinner); } ArrayAdapter<String> winnerAdapter, loserAdapter; if (t.getWinnerPoints() > 0) { List<Player> players = t.getWinnerPlayers(); String[] playerNames = new String[players.size()]; for (int i = 0; i < players.size(); i++) { playerNames[i] = players.get(i).getName(); } winnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, playerNames); winnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); int pos = players.indexOf(t.getWinnerPlayer()); winnerSpinner.setAdapter(winnerAdapter); winnerSpinner.setSelection(pos); if (playerNames.length > 0) { winnerSpinner.setEnabled(true); } else { winnerSpinner.setEnabled(false); } } else { winnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[0]); winnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); winnerSpinner.setAdapter(winnerAdapter); winnerSpinner.setEnabled(false); } if (t.getLoserPoints() < 0) { List<Player> players = t.getLoserPlayers(); String[] playerNames = new String[players.size()]; for (int i = 0; i < players.size(); i++) { playerNames[i] = players.get(i).getName(); } loserAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, playerNames); loserAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); int pos = players.indexOf(t.getLoserPlayer()); loserSpinner.setAdapter(loserAdapter); loserSpinner.setSelection(pos); if (playerNames.length > 0) { loserSpinner.setEnabled(true); } else { loserSpinner.setEnabled(false); } } else { loserAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[0]); loserAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); loserSpinner.setAdapter(loserAdapter); loserSpinner.setEnabled(false); } modifyingSpinner = false; }
@Override public void onCheckedChanged(RadioGroup rg, int checkedId) { if (modifyingRadioButtons) { return; } modifyingRadioButtons = true; RadioGroup teamARG = (RadioGroup) findViewById(R.id.teamaradiogroup); RadioGroup teamBRG = (RadioGroup) findViewById(R.id.teambradiogroup); RadioButton teamARB = (RadioButton) findViewById(teamARG.getCheckedRadioButtonId()); RadioButton teamBRB = (RadioButton) findViewById(teamBRG.getCheckedRadioButtonId()); int aPoints = Integer.parseInt(teamARB.getText().toString()); int bPoints = Integer.parseInt(teamBRB.getText().toString()); // somebody needs to be reset to 0 if ((aPoints > 0 && bPoints > 0) || (aPoints < 0 && bPoints < 0)) { // team A was clicked, so team B should reset if (rg.equals(teamARG)) { bPoints = 0; ((RadioButton) findViewById(R.id.teambradio0)).setChecked(true); } // team B was clicked, so team A should reset else { aPoints = 0; ((RadioButton) findViewById(R.id.teamaradio0)).setChecked(true); } } if (aPoints > 0 || bPoints > 0) { enableBonusBoxes(); } else { clearBonusBoxes(); disableBonusBoxes(); } Tossup oldTossup = game.currTossup(); Tossup t; if (aPoints >= bPoints) { t = new Tossup( game.getCurrTossupNum(), game.getTeamA(), aPoints, game.getTeamB(), bPoints, oldTossup.isTiebreaker()); } else { t = new Tossup( game.getCurrTossupNum(), game.getTeamB(), bPoints, game.getTeamA(), aPoints, oldTossup.isTiebreaker()); } game.updateCurrTossup(t); updateGlobalScore(); updatePlayerSpinner(); updateBonusBoxes(t); modifyingRadioButtons = false; }