小编Ali*_*med的帖子

如何将 dataBinding 与 spinner 一起使用并获取当前值?

我正在使用 MVVM 和数据绑定( Kotlin )开发一个 Android 应用程序,我想知道如何轻松获取 ViewModel 中微调器的当前值并轻松处理它。我知道这个问题被问了很多次,但实际上我没有得到有用的答案,所以你能帮助大家吗?感谢你的帮助。谢谢。


<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="registerviewmodel"
            type="com.AAA.BBB.SignUpAuthViewModel" />
    </data>
<Spinner
                android:id="@+id/citySpinnerSignUpLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:entries="@array/cities"
                />

    </ScrollView>
</layout>
Run Code Online (Sandbox Code Playgroud)
class SignUpAuthViewModel : ViewModel() {

    var username: String? = null
    var password: String? = null
    var town: String? = null
    var age: String? = null
    var phone: String? = null
    var passwordConfirmation: String? = null



//this variable should hold the city of the user
// from spinner of cities …
Run Code Online (Sandbox Code Playgroud)

xml data-binding android spinner kotlin

3
推荐指数
1
解决办法
6701
查看次数

此错误的原因是什么以及如何修复?

我正在使用带有Patternkotlin的 android 系统。 发送和获取 api 调用。databindingMVVMRetrofit2

当我发送登录呼叫时,我总是崩溃,这是消息:

java.lang.IllegalArgumentException: Unable to create call adapter for class java.lang.Object
Run Code Online (Sandbox Code Playgroud)

我多次重新设计了响应,它实际上与我的 api 的响应相匹配。有什么帮助吗?

//my retrofit interface

interface RetrofitInterface {

    @FormUrlEncoded
    @POST("userLogin")
    suspend fun userLogin(
        @Field("userName") userName: String,
        @Field("userPassword") userPassword: String
    ): Response<UserLoginResponse>



    companion object {
        operator fun invoke(
            networkConnectionInterceptor: NetworkConnectionInterceptor
        ): RetrofitInterface {
            val okHttpClient = OkHttpClient.Builder()
                .addInterceptor(networkConnectionInterceptor)
                .build()

            return Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl("http://muUrlIsCorrect/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(RetrofitInterface::class.java)

        }
    }

}

//My response class
data class UserLoginResponse(
    var error: Boolean?,
    var …
Run Code Online (Sandbox Code Playgroud)

data-binding api android kotlin retrofit2

2
推荐指数
1
解决办法
3384
查看次数

标签 统计

android ×2

data-binding ×2

kotlin ×2

api ×1

retrofit2 ×1

spinner ×1

xml ×1