视图不会与数据绑定

views will not bind with data

我正在使用视图模型和数据绑定。我有一些从服务器获取的数据,应该传递给 ui。我写了这个视图模型 class:



class ProfileViewModel(val context: Context) : ViewModel() {

    private val viewModelJob = Job()
    private val scope = CoroutineScope(Dispatchers.Main + viewModelJob)
    val name = MutableLiveData<String>()
    val subscription = MutableLiveData<String>()
    val followers = MutableLiveData<String>()
    val following = MutableLiveData<String>()
    val signals = MutableLiveData<String>()
    val image = MutableLiveData<String>()


    fun getUserInfo() {
        scope.launch {
            withContext(Dispatchers.Default) {
                val request =
                    JsonObjectRequest(Request.Method.POST, Address().UserInfoAPI, null,
                        Response.Listener {
                            name.value = it.getString("name")
                            image.value = it.getString("image")
                            subscription.value = it.getString("subscription")
                            followers.value = it.getString("followers")
                            following.value = it.getString("following")
                            signals.value = it.getString("signals")
                        }, Response.ErrorListener {
                            Log.i("Log", "error $it")
                        })

                val queue = Volley.newRequestQueue(context)
                queue.add(request)
            }
        }
    }


    


    override fun onCleared() {
        super.onCleared()
        scope.cancel()
    }
}

我在我的 xml 布局中使用了这个视图模型,如下所示:

<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="viewModel"
            type="com.app.main.profile.ProfileViewModel" />
    </data>



<!--- this is just one sample of my elements------>
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{viewModel.name}"
            android:textAppearance="@style/vazir_font"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/avatarHolder" />


</layout>

这是我的片段代码:


class ProfileFragment : Fragment() {

    private lateinit var binding: FragmentProfileBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_profile, container, false)
        return binding.root
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val factory = ProfileViewModelFactory(context!!)
        val viewModel = ViewModelProvider(this, factory).get(ProfileViewModel::class.java)

        viewModel.getUserInfo()
        binding.viewModel = viewModel


        binding.exit.setOnClickListener {
            Utils(context!!).isLoggedIn = false
        }
    }


}

我的问题是 TextViews 不会填充服务器响应,它们仍然是空的。 我该如何解决这个问题?

试试看, 我想你忘了,nameMutableLiveData。如果我们要访问那么需要name.value.

<TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{viewModel.name.value}"
                android:textAppearance="@style/vazir_font"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/avatarHolder" />

binding = DataBindingUtil.inflate(inflater, R.layout.fragment_profile, container, false)之后尝试添加以下内容:

binding.lifecycleOwner = this

应该可以解决问题。