@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);
            }
          }
        }
  private void updatePointsSelector(Tossup t) {
    Team winner = t.getWinnerTeam();
    RadioGroup winnerGroup;
    RadioGroup loserGroup;

    if (winner.equals(game.getTeamA())) {
      winnerGroup = (RadioGroup) findViewById(R.id.teamaradiogroup);
      loserGroup = (RadioGroup) findViewById(R.id.teambradiogroup);
    } else {
      winnerGroup = (RadioGroup) findViewById(R.id.teambradiogroup);
      loserGroup = (RadioGroup) findViewById(R.id.teamaradiogroup);
    }
    this.modifyingRadioButtons = true;
    switch (t.getWinnerPoints()) {
      case 15:
        ((RadioButton) winnerGroup.getChildAt(0)).setChecked(true);
        enableBonusBoxes();
        break;
      case 10:
        ((RadioButton) winnerGroup.getChildAt(1)).setChecked(true);
        enableBonusBoxes();
        break;
      case 0:
        ((RadioButton) winnerGroup.getChildAt(2)).setChecked(true);
        disableBonusBoxes();
        break;
      case -5:
        throw new RuntimeException("Winner can't have -5");
      default:
        throw new RuntimeException("Invalid point value for winner");
    }

    switch (t.getLoserPoints()) {
      case 0:
        ((RadioButton) loserGroup.getChildAt(2)).setChecked(true);
        break;
      case -5:
        ((RadioButton) loserGroup.getChildAt(3)).setChecked(true);
        break;
      default:
        // this should never happen
        ((RadioButton) loserGroup.getChildAt(2)).setChecked(true);
        break;
    }
    this.modifyingRadioButtons = false;
  }
  @Override
  protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    AlertDialog.Builder alertBuilder;
    final CharSequence[] items = {teamAName, teamBName};

    switch (id) {
      case NEW_GAME_DIALOG:
        alertBuilder = new AlertDialog.Builder(this);
        alertBuilder
            .setMessage("Are you sure you want to start a new game?")
            .setCancelable(false)
            .setPositiveButton(
                "Yes",
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                    NAQTScoresheet.this.startNewGame();
                  }
                })
            .setNegativeButton(
                "No",
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                  }
                });
        dialog = alertBuilder.create();
        break;
      case LOAD_GAME_DIALOG:
        alertBuilder = new AlertDialog.Builder(this);
        alertBuilder
            .setMessage("Are you sure you want to leave this game?")
            .setCancelable(false)
            .setPositiveButton(
                "Yes",
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                    Intent intent = new Intent(NAQTScoresheet.this, LoadGameScreen.class);
                    startActivityForResult(intent, LOAD_GAME_RESULT);
                  }
                })
            .setNegativeButton(
                "No",
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                  }
                });
        dialog = alertBuilder.create();
        break;
      case CHOOSE_TEAM_ADD_DIALOG:
        alertBuilder = new AlertDialog.Builder(this);
        alertBuilder.setTitle("Pick a team");
        alertBuilder.setItems(
            items,
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int item) {
                dialog.dismiss();
                NAQTScoresheet.this.removeDialog(CHOOSE_TEAM_ADD_DIALOG);
                if (items[item].equals(game.getTeamA().getName())) {
                  NAQTScoresheet.this.showDialog(CHOOSE_TEAM_A_PLAYER_NAME_DIALOG);
                } else {
                  NAQTScoresheet.this.showDialog(CHOOSE_TEAM_B_PLAYER_NAME_DIALOG);
                }
              }
            });
        alertBuilder.setOnCancelListener(
            new DialogInterface.OnCancelListener() {
              @Override
              public void onCancel(DialogInterface dialog) {
                NAQTScoresheet.this.removeDialog(CHOOSE_TEAM_ADD_DIALOG);
              }
            });
        dialog = alertBuilder.create();
        break;
      case CHOOSE_TEAM_A_PLAYER_NAME_DIALOG:
        final Dialog finalDialog = new Dialog(NAQTScoresheet.this);
        dialog = finalDialog;
        dialog.setContentView(R.layout.add_player_dialog);
        dialog.setTitle("Enter Player Name");
        dialog.setCancelable(true);
        Button b = (Button) dialog.findViewById(R.id.addplayerbutton);
        b.setOnClickListener(
            new OnClickListener() {
              private Dialog parentDialog = finalDialog;

              @Override
              public void onClick(View view) {
                EditText textbox = (EditText) parentDialog.findViewById(R.id.newplayername);
                String playerName = textbox.getText().toString();
                Player newPlayer = new Player(playerName);
                NAQTScoresheet.this.game.getTeamA().addPlayer(newPlayer);
                NAQTScoresheet.this.addToRemainingTossups(game.getTeamA(), newPlayer);
                parentDialog.dismiss();
                NAQTScoresheet.this.removeDialog(CHOOSE_TEAM_A_PLAYER_NAME_DIALOG);
                NAQTScoresheet.this.updatePlayerSpinner();
              }
            });
        break;
      case CHOOSE_TEAM_B_PLAYER_NAME_DIALOG:
        final Dialog bFinalDialog = new Dialog(NAQTScoresheet.this);
        dialog = bFinalDialog;
        dialog.setContentView(R.layout.add_player_dialog);
        dialog.setTitle("Enter Player Name");
        dialog.setCancelable(true);
        Button bButton = (Button) dialog.findViewById(R.id.addplayerbutton);
        bButton.setOnClickListener(
            new OnClickListener() {
              private Dialog parentDialog = bFinalDialog;

              @Override
              public void onClick(View view) {
                EditText textbox = (EditText) parentDialog.findViewById(R.id.newplayername);
                String playerName = textbox.getText().toString();
                Player newPlayer = new Player(playerName);
                NAQTScoresheet.this.game.getTeamB().addPlayer(newPlayer);
                NAQTScoresheet.this.addToRemainingTossups(game.getTeamB(), newPlayer);
                parentDialog.dismiss();
                NAQTScoresheet.this.removeDialog(CHOOSE_TEAM_B_PLAYER_NAME_DIALOG);
                NAQTScoresheet.this.updatePlayerSpinner();
              }
            });
        break;
      case TEAM_A_NAME_DIALOG:
        final Dialog teamANameDialog = new Dialog(NAQTScoresheet.this);
        dialog = teamANameDialog;
        dialog.setContentView(R.layout.team_name_dialog);
        dialog.setTitle("Enter Team Name");
        dialog.setCancelable(true);
        Button teamANameButton = (Button) dialog.findViewById(R.id.teamnamebutton);
        teamANameButton.setOnClickListener(
            new OnClickListener() {
              private Dialog parentDialog = teamANameDialog;

              @Override
              public void onClick(View view) {
                EditText textbox = (EditText) parentDialog.findViewById(R.id.teamnameeditext);
                String teamName = textbox.getText().toString();
                TextView tv = (TextView) NAQTScoresheet.this.findViewById(R.id.teama);
                tv.setText(teamName);
                teamAName = teamName;
                parentDialog.dismiss();
                NAQTScoresheet.this.removeDialog(TEAM_A_NAME_DIALOG);
                game.getTeamA().setName(teamAName);
              }
            });
        break;
      case TEAM_B_NAME_DIALOG:
        final Dialog teamBNameDialog = new Dialog(NAQTScoresheet.this);
        dialog = teamBNameDialog;
        dialog.setContentView(R.layout.team_name_dialog);
        dialog.setTitle("Enter Team Name");
        dialog.setCancelable(true);
        Button teamBNameButton = (Button) dialog.findViewById(R.id.teamnamebutton);
        teamBNameButton.setOnClickListener(
            new OnClickListener() {
              private Dialog parentDialog = teamBNameDialog;

              @Override
              public void onClick(View view) {
                EditText textbox = (EditText) parentDialog.findViewById(R.id.teamnameeditext);
                String teamName = textbox.getText().toString();
                TextView tv = (TextView) NAQTScoresheet.this.findViewById(R.id.teamb);
                tv.setText(teamName);
                teamBName = teamName;
                game.getTeamB().setName(teamBName);
                parentDialog.dismiss();
                NAQTScoresheet.this.removeDialog(TEAM_B_NAME_DIALOG);
              }
            });
        break;
      case CHOOSE_TEAM_REMOVE_DIALOG:
        alertBuilder = new AlertDialog.Builder(this);
        alertBuilder.setTitle("Pick a team");
        alertBuilder.setItems(
            items,
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int item) {
                dialog.dismiss();
                NAQTScoresheet.this.removeDialog(CHOOSE_TEAM_REMOVE_DIALOG);
                if (items[item].equals(game.getTeamA().getName())) {
                  NAQTScoresheet.this.showDialog(CHOOSE_TEAM_A_PLAYERS_DIALOG);
                } else {
                  NAQTScoresheet.this.showDialog(CHOOSE_TEAM_B_PLAYERS_DIALOG);
                }
              }
            });
        dialog = alertBuilder.create();
        break;
      case CHOOSE_TEAM_A_PLAYERS_DIALOG:
        final Player[] players = game.getTeamA().getPlayers().toArray(new Player[0]);
        final CharSequence[] playerNames = new CharSequence[players.length];
        for (int i = 0; i < playerNames.length; i++) {
          playerNames[i] = players[i].getName();
        }
        alertBuilder = new AlertDialog.Builder(this);
        alertBuilder.setTitle("Pick a player");
        alertBuilder.setItems(
            playerNames,
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int idx) {
                NAQTScoresheet.this.removeFromRemainingTossups(
                    game.getTeamA(), playerNames[idx].toString());
                game.getTeamA().removePlayer(playerNames[idx].toString());
                dialog.dismiss();
                NAQTScoresheet.this.removeDialog(CHOOSE_TEAM_A_PLAYERS_DIALOG);
                NAQTScoresheet.this.updatePlayerSpinner();
              }
            });
        dialog = alertBuilder.create();
        break;
      case CHOOSE_TEAM_B_PLAYERS_DIALOG:
        final Player[] bPlayers = game.getTeamB().getPlayers().toArray(new Player[0]);
        final CharSequence[] bPlayerNames = new CharSequence[bPlayers.length];
        for (int i = 0; i < bPlayerNames.length; i++) {
          bPlayerNames[i] = bPlayers[i].getName();
        }
        alertBuilder = new AlertDialog.Builder(this);
        alertBuilder.setTitle("Pick a player");
        alertBuilder.setItems(
            bPlayerNames,
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int idx) {
                NAQTScoresheet.this.removeFromRemainingTossups(
                    game.getTeamB(), bPlayerNames[idx].toString());
                game.getTeamB().removePlayer(bPlayerNames[idx].toString());
                dialog.dismiss();
                NAQTScoresheet.this.removeDialog(CHOOSE_TEAM_B_PLAYERS_DIALOG);
                NAQTScoresheet.this.updatePlayerSpinner();
              }
            });
        dialog = alertBuilder.create();
        break;
      case UPLOAD_GAME_DIALOG:
        final Dialog uploadGameDialog = new Dialog(NAQTScoresheet.this);
        dialog = uploadGameDialog;
        dialog.setContentView(R.layout.upload_game_dialog);
        dialog.setTitle("Enter Upload URL");
        dialog.setCancelable(true);
        if (this.savedExportURL != null) {
          EditText textbox = (EditText) dialog.findViewById(R.id.uploadgameedittext);
          textbox.setText(this.savedExportURL);
        }
        if (this.savedExportFormat != null) {
          RadioButton rb;
          if (this.savedExportFormat == DataExport.Format.XML) {
            rb = (RadioButton) dialog.findViewById(R.id.xmlbutton);
          } else {
            rb = (RadioButton) dialog.findViewById(R.id.jsonbutton);
          }
          rb.toggle();
        }
        Button uploadGameButton = (Button) dialog.findViewById(R.id.uploadgamebutton);
        uploadGameButton.setOnClickListener(
            new OnClickListener() {
              private Dialog parentDialog = uploadGameDialog;

              @Override
              public void onClick(View view) {
                EditText textbox = (EditText) parentDialog.findViewById(R.id.uploadgameedittext);
                String remoteURL = textbox.getText().toString();
                RadioGroup formatGroup =
                    (RadioGroup) parentDialog.findViewById(R.id.uploadformatradiogroup);
                RadioButton rb =
                    (RadioButton) parentDialog.findViewById(formatGroup.getCheckedRadioButtonId());
                DataExport.Format format;
                if (rb.getId() == R.id.xmlbutton) {
                  format = DataExport.Format.XML;
                } else {
                  format = DataExport.Format.JSON;
                }
                NAQTScoresheet.this.savedExportURL = remoteURL;
                NAQTScoresheet.this.savedExportFormat = format;
                DataExport.postGameData(
                    getApplicationContext(), NAQTScoresheet.this.game, remoteURL, format);
                parentDialog.dismiss();
                NAQTScoresheet.this.removeDialog(UPLOAD_GAME_DIALOG);
              }
            });
        break;
      default:
        dialog = null;
        break;
    }
    return dialog;
  }
        @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;
        }