没有收到 mutableLivedata android 的回调?
Not getting callback for mutableLivedata android?
在我的视图模型中,我有 2 个 api 调用 returns 同一个对象。但是我创建了 2 个不同的 MutableLiveData 对象,但我无法观察到第二个对象。
这是我在片段中的代码
private fun initObservables() {
holidayViewModel.progressDialog?.observe(this, Observer {
if (it!!) customeProgressDialog?.show() else customeProgressDialog?.dismiss()
})
holidayViewModel.apiResponse?.observe(
viewLifecycleOwner,
androidx.lifecycle.Observer { response ->
if (response.dataList != null) {
response.dataList!!.removeAt(0)
if (requireArguments().getString("file_type")
.equals(NetworkConstant.FILE_TYPE_LOH, ignoreCase = true)
) {
val data = Data()
data.CountryId = "0"
data.CountryName = "Main organisation"
response.dataList!!.add(0, data)
}
val holidayAdapter = CountryAdapter(response.dataList)
binding.holiday.adapter = holidayAdapter
holidayAdapter.notifyDataSetChanged()
holidayAdapter.setListener(this)
}
})
holidayViewModel.pdfLink?.observe(
viewLifecycleOwner,
androidx.lifecycle.Observer { response ->
utils.openPdf(response.dataList!!.get(0)?.filePath)
})
}
这是视图模型class
class HolidayViewModel(networkCall: NetworkCall) : ViewModel() {
var progressDialog: SingleLiveEvent<Boolean>? = null
var apiResponse: MutableLiveData<ApiResponse>? = null
var pdfLink: MutableLiveData<ApiResponse>? = null
var networkCall: NetworkCall;
init {
progressDialog = SingleLiveEvent<Boolean>()
apiResponse = MutableLiveData<ApiResponse>()
this.networkCall = networkCall
}
fun countries(username: String?, userId: String?) {
progressDialog?.value = true
val apiPost = ApiPost()
apiPost.userName = username
apiPost.UserId = userId
networkCall.getCountries(apiPost).enqueue(object : Callback<ApiResponse?> {
override fun onResponse(
call: Call<ApiResponse?>,
response: Response<ApiResponse?>
) {
progressDialog?.value = false
apiResponse?.value = response.body()
}
override fun onFailure(
call: Call<ApiResponse?>,
t: Throwable
) {
progressDialog?.value = false
}
})
}
fun fetchPdf(
username: String?,
password: String?,
userId: String?,
countryId: String?,
fileType: String?
) {
progressDialog?.value = true
val apiPost = ApiPost()
apiPost.userName = username
apiPost.password = password
apiPost.UserId = userId
apiPost.CountryId = countryId
apiPost.FileType = fileType
networkCall.getPDF(apiPost).enqueue(object : Callback<ApiResponse?> {
override fun onResponse(
call: Call<ApiResponse?>,
response: Response<ApiResponse?>
) {
progressDialog?.value = false
pdfLink?.value = response.body()
}
override fun onFailure(
call: Call<ApiResponse?>,
t: Throwable
) {
progressDialog?.value = false
}
})
}
}
我正在尝试观察 pdfLink
对象,但是调用了 API 但我从未在我的片段中获得此对象的回调。
这里有什么问题?
一个愚蠢的错误忘记初始化它
pdfLink = MutableLiveData<ApiResponse>()
问题是 pdfLink
在 viewModel 中总是 null。
您已声明 var pdfLink: MutableLiveData<ApiResponse>? = null
但尚未初始化。因为你用 ?
检查它是空的,它永远不会抛出异常。
试试这个:
init {
progressDialog = SingleLiveEvent<Boolean>()
apiResponse = MutableLiveData<ApiResponse>()
pdfLink = MutableLiveData<ApiResponse>() // Add this line inside init
this.networkCall = networkCall
}
在我的视图模型中,我有 2 个 api 调用 returns 同一个对象。但是我创建了 2 个不同的 MutableLiveData 对象,但我无法观察到第二个对象。
这是我在片段中的代码
private fun initObservables() {
holidayViewModel.progressDialog?.observe(this, Observer {
if (it!!) customeProgressDialog?.show() else customeProgressDialog?.dismiss()
})
holidayViewModel.apiResponse?.observe(
viewLifecycleOwner,
androidx.lifecycle.Observer { response ->
if (response.dataList != null) {
response.dataList!!.removeAt(0)
if (requireArguments().getString("file_type")
.equals(NetworkConstant.FILE_TYPE_LOH, ignoreCase = true)
) {
val data = Data()
data.CountryId = "0"
data.CountryName = "Main organisation"
response.dataList!!.add(0, data)
}
val holidayAdapter = CountryAdapter(response.dataList)
binding.holiday.adapter = holidayAdapter
holidayAdapter.notifyDataSetChanged()
holidayAdapter.setListener(this)
}
})
holidayViewModel.pdfLink?.observe(
viewLifecycleOwner,
androidx.lifecycle.Observer { response ->
utils.openPdf(response.dataList!!.get(0)?.filePath)
})
}
这是视图模型class
class HolidayViewModel(networkCall: NetworkCall) : ViewModel() {
var progressDialog: SingleLiveEvent<Boolean>? = null
var apiResponse: MutableLiveData<ApiResponse>? = null
var pdfLink: MutableLiveData<ApiResponse>? = null
var networkCall: NetworkCall;
init {
progressDialog = SingleLiveEvent<Boolean>()
apiResponse = MutableLiveData<ApiResponse>()
this.networkCall = networkCall
}
fun countries(username: String?, userId: String?) {
progressDialog?.value = true
val apiPost = ApiPost()
apiPost.userName = username
apiPost.UserId = userId
networkCall.getCountries(apiPost).enqueue(object : Callback<ApiResponse?> {
override fun onResponse(
call: Call<ApiResponse?>,
response: Response<ApiResponse?>
) {
progressDialog?.value = false
apiResponse?.value = response.body()
}
override fun onFailure(
call: Call<ApiResponse?>,
t: Throwable
) {
progressDialog?.value = false
}
})
}
fun fetchPdf(
username: String?,
password: String?,
userId: String?,
countryId: String?,
fileType: String?
) {
progressDialog?.value = true
val apiPost = ApiPost()
apiPost.userName = username
apiPost.password = password
apiPost.UserId = userId
apiPost.CountryId = countryId
apiPost.FileType = fileType
networkCall.getPDF(apiPost).enqueue(object : Callback<ApiResponse?> {
override fun onResponse(
call: Call<ApiResponse?>,
response: Response<ApiResponse?>
) {
progressDialog?.value = false
pdfLink?.value = response.body()
}
override fun onFailure(
call: Call<ApiResponse?>,
t: Throwable
) {
progressDialog?.value = false
}
})
}
}
我正在尝试观察 pdfLink
对象,但是调用了 API 但我从未在我的片段中获得此对象的回调。
这里有什么问题?
一个愚蠢的错误忘记初始化它
pdfLink = MutableLiveData<ApiResponse>()
问题是 pdfLink
在 viewModel 中总是 null。
您已声明 var pdfLink: MutableLiveData<ApiResponse>? = null
但尚未初始化。因为你用 ?
检查它是空的,它永远不会抛出异常。
试试这个:
init {
progressDialog = SingleLiveEvent<Boolean>()
apiResponse = MutableLiveData<ApiResponse>()
pdfLink = MutableLiveData<ApiResponse>() // Add this line inside init
this.networkCall = networkCall
}