我通过翻新从API调用中获取以下模型:
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>
)
出于与this SO答案相关的原因,我需要添加一个额外的属性。
额外的参数将是数据类PaymentsMethods:
data class PaymentsMethods(
val paymentMethods: List<PaymentMethod>
)
创建属性值时如何将其添加到User类?
这是我到目前为止尝试过的:
// add an attribute (with a default value) that won't be filled by the parser.
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>,
@Expose var paymentsObject: PaymentMethods = PaymentMethods(payments)
)
和
// create variable and set the value on init
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>
){
init {
var paymentsObject: PaymentMethods = PaymentMethods(payments)
}
}
它们都不起作用。任何建议将不胜感激。
答案 0 :(得分:0)
我希望这是您需要的:
data class User(
@Expose val id: Int,
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?
) {
@Expose var payments: List<PaymentMethod> by Delegates.observable(listOf()) {
prop, oldValue, newValue ->
paymentsObject = PaymentMethods(newValue)
}
lateinit var paymentsObject: PaymentMethods
}