Kotlin and Android Development featuring Jetpack: Unique constraint error when running chapter 5 code changes

Apologies for losing track of this thread, but on the plus side, I see what’s going on. It took me a bit to find, too, since it wasn’t clear right away.

Quick version:
You’re missing android:text="@={player.playerName}" on the @+id/edit_text_player_name <Edit Text> in player_list_item.xml.

Long version:
The database exception happens because the game sees you as trying to enter in the same player twice, meaning both players have the same ID. They both are seen as having the same ID because of how the app gets players in PennyDropDao:

// PennyDropDao.startGame(...)
val playerIds = players.map { player -> 
    getPlayer(player.playerName)?.playerId ?: insertPlayer(player)
}

We get players by name from the DB or create new versions. If two players have the same name (even if it’s a blank name), one is inserted into the DB and the other is retrieved right away, but they’re both the same.

In this issue’s case, the names were blank because nothing was in place to assign the name to the playerName field. That lives inside player_list_item.xml and the edit_text_player_name <EditText>.

If you don’t have a value set for android:text, a TextView will still show what you type in, but nothing happens with it. You need to set an expression there (with the = after the @) to link it back to the NewPlayer object:

android:text="@={player.playerName}"

While the error makes sense in the end, it’s almost misleading given where the issue starts out.