簡體   English   中英

DataBinding 在第一次遠程調用時顯示 null 值

[英]DataBinding showing null value on first remote call

在我的應用程序中,我有一個片段調用遠程服務來獲取用戶配置文件信息並顯示它,並且我使用 DataBinding 來顯示數據。

這是我的布局:

<?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">

    <data>
        <variable
            name="viewModel"
            type="com.myapp.ProfileViewModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{viewModel.profile.firstName+ ' '+ viewModel.profile.lastName}" />

            <!-- Other textviews -->

    </LinearLayout>

</layout>

這是 ProfileViewModel class

class ProfileViewModel : ViewModel() {

    @Inject
    lateinit var profileRepository: ProfileRepository

    private var _profile = MutableLiveData<Profile>()
    val profile: LiveData<Profile>
        get() = _profile

    fun getProfile(token: String) {
        profileRepository.profile(
            token,
            {
                // success
                _profile.value = it.value
            },
            {
               //error
            }
        )
    }
}

data class Profile(
    firstName : String,
    lastName : String,
    // other fields
)

這是應該顯示配置文件的片段:

class ProfileFragment : Fragment() {

    private lateinit var binding: FragmentProfileBinding
    private lateinit var viewModel: ProfileViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_profile,
            container,
            false
        )

        viewModel = activity?.run {
            ViewModelProviders.of(this)[ProfileViewModel::class.java]
        } ?: throw Exception("Invalid Activity")

        binding.viewModel = viewModel

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel.getProfile(
            "aToken"
        )
    }
}

現在它發生在我第一次打開片段,存儲庫調用服務並正確獲取數據時,但在 textviews 中顯示“null”。 如果我關閉片段並重新打開它,edittext 將正確填充。 我的代碼有什么問題?

在片段 class 中設置 binding.lifecycleOwner,以便 LiveData 對象中的更新反映在相應的視圖中。 您的片段 class 應如下所示:

class ProfileFragment : Fragment() {

    private lateinit var binding: FragmentProfileBinding
    private lateinit var viewModel: ProfileViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_profile,
            container,
            false
        )

        viewModel = activity?.run {
            ViewModelProviders.of(this)[ProfileViewModel::class.java]
        } ?: throw Exception("Invalid Activity")

        binding.viewModel = viewModel 

        //add lifecycleOwner
        binding.lifecycleOwner = this

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel.getProfile(
            "aToken"
        )
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM