Example #1
0
  private List<Game> convertToGames(Cursor cursor) {
    List<Game> result = new ArrayList<Game>();

    Game currentGame = null;

    while (cursor.moveToNext()) {

      int currentId = cursor.getInt(0);

      if (currentGame == null || currentGame.getId() != currentId) { // new
        // Game

        currentGame = new Game();
        currentGame.setId(currentId);
        currentGame.setDateStarted(cursor.getLong(1));
        currentGame.setDateSaved(cursor.getLong(2));
        currentGame.setName(cursor.getString(3));
        result.add(currentGame);
      }

      List<PlayerScore> playerScores = new ArrayList<PlayerScore>();

      // build up all the PlayerScores
      do {

        if (cursor.getInt(0) != currentId) {
          cursor.moveToPrevious(); // went too far
          break;
        }

        PlayerScore playerScore = new PlayerScore();

        playerScore.setId(cursor.getInt(4));
        playerScore.setName(cursor.getString(5));
        playerScore.setScore(cursor.getLong(6));
        playerScore.setPlayerNumber(cursor.getInt(7));
        playerScore.setHistory(
            Delta.fromJoinedStrings(
                StringUtil.nullToEmpty(cursor.getString(8)),
                StringUtil.nullToEmpty(cursor.getString(9))));
        playerScore.setLastUpdate(cursor.getLong(10));
        playerScore.setPlayerColor(PlayerColor.deserialize(cursor.getString(11)));
        playerScores.add(playerScore);

      } while (cursor.moveToNext());

      Collections.sort(playerScores, PlayerScore.sortByPlayerNumber());

      currentGame.setPlayerScores(playerScores);
    }

    return result;
  }