在我的数据库中,我有一个名为的表Account
,看起来有点像这样
@Entity(tableName = "accounts", primaryKeys = ["server_id", "account_id"])
data class Account(
@ColumnInfo(name = "server_id")
val serverId: Long,
@ColumnInfo(name = "account_id")
val accountId: Int,
@ColumnInfo(name = "first_name", defaultValue = "")
var firstname: String
)
所以假设我们有以下数据库快照
server_id account_id first_name 1 10 Zak 1 11 Tom 1 12 Bob 1 13 Jim 1 14 Mike
现在我也有以下 POJO,它代表 chatRoom 内的可用视频室
data class RoomInfo(
@SerializedName("m")
val participantIntList: List<Int>,
@SerializedName("o")
val roomId: String,
@SerializedName("s")
val status: Int
)
所以我从我的 Socket 收到一个传入的响应,如下所示
[
{"m": [10, 11, 12], "o": "room_technical", "s": 1},
{"m": [13, 14], "o": "room_operation", "s": 1}
]
我将它映射到一个列表中,所以我有
val roomInfo: LiveData<List<RoomInfo>> = socketManager.roomInfo
// So the value is basically the json converted to a list of RoomInfos using Gson
为了向用户显示这个可用的房间列表,我需要将 m(即现在房间内的成员)从 accountIds 转换为 account.firstnames。
所以我最终想要的是一个名为 RoomInfoItem 的新对象的列表,它将保存房间列表,其中 accountIds 从数据库的 Account 表中转换为 firstNames。
data class RoomInfoItem(
val roomInfo: RoomInfo,
val participantNames: List<String>
)
因此,如果我们进行转换,我们需要得到以下结果
RoomInfo (
// RoomInfo
{"m": [10, 11, 12], "o": "room_technical", "s": 1},
// Participant names
["Zak", "Tom", "Bob"]
)
RoomInfo (
// RoomInfo
{"m": [13, 14], "o": "room_operation", "s": 1},
// Participant names
["Jim", "Mike"]
)
我的活动需要使用 RoomInfoItems 观察 LiveData,所以我想要的是 LiveData<List> 以将其转换为 LiveData<List>。我怎样才能做到这一点?