我使用的是View模型和DataBinding。我有一些数据是从服务器上获取的,应该传递给ui。我在那里写了这个视图模型类:
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不会填充服务器响应,它们仍然是空的。我该如何解决这个问题?
在binding = DataBindingUtil.inflate(inflater, R.layout.fragment_profile, container, false)
之后尝试添加以下内容:
binding.lifecycleOwner = this
它应该能解决这个问题。
试试吧,我想你忘了,name
就是MutableLiveData
。如果我们想访问,那么需要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" />