Kotlin ViewModel类中需要的属性获取器或设置器

时间:2018-12-24 21:40:24

标签: android kotlin android-viewmodel

我有以下ViewModel类:

class PersonViewModel(
        context: Application,
        private val dataSource: MoviesRemoteDataSource)
    : AndroidViewModel(context) {

    internal val compositeDisposable = CompositeDisposable()
    val person: ObservableField<Person>()
    private val isVisible = ObservableBoolean(false)

    fun showPerson(personId: String) {
        val personSubscription = dataSource.getPerson(personId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ person ->
                    isVisible.set(true)
                    this.person.set(person)
                }
                ) { throwable -> Timber.e(throwable) }

        compositeDisposable.add(personSubscription)
    }
}

这是Person类:

class Person(
        @SerializedName("birthday")
        var birthDay: String?,
        @SerializedName("deathday")
        var deathDay: String?,
        var id: Int,
        @SerializedName("also_known_as")
        var alsoKnowAs: List<String>,
        var biography: String,
        @SerializedName("place_of_birth")
        var placeOfBirth: String?)

它在ViewModel的这一行显示错误:

val person: ObservableField<Person>()

它说:property getter or setter expected

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

最有可能替换:

val person: ObservableField<Person>()

具有:

val person = ObservableField<Person>()

这会将person设置为使用您创建的ObservableField<Person>进行初始化。

答案 1 :(得分:0)

查看您的代码:

val person: ObservableField<Person>()

您有一个简单的语法错误,键入后输入括号!!删除它们或更改冒号以分配符号:

val person: ObservableField<Person>

val person = ObservableField<Person>()