Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] kotlin – Not getting callback for mutableLivedata android?

Posted on November 11, 2022 By

Solution 1 :

The problem is pdfLink is always null in viewModel.

You’ve declared var pdfLink: MutableLiveData<ApiResponse>? = null but haven’t initialized yet. And since you are null checking it with ?, it never throws exception.

Try this:

init {
    progressDialog = SingleLiveEvent<Boolean>()
    apiResponse = MutableLiveData<ApiResponse>()
    pdfLink = MutableLiveData<ApiResponse>() // Add this line inside init
    this.networkCall = networkCall
}

Solution 2 :

A silly mistake forget to initialize it

pdfLink = MutableLiveData<ApiResponse>()

Problem :

In my viewmodel I have 2 api calls which returns same object. However I created 2 different MutableLiveData objects but I am not able to observe the 2nd object.

This is my code in fragment

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)
        })
}

This is the viewmodel 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
        }
    })
   }


 }

I am trying to observe pdfLink object , however the API is called but I never get the callback in my fragment for this object.

READ  [FIXED] How to change permission dialog language in the react-native android project?
Powered by Inline Related Posts

What is wrong here?

Comments

Comment posted by Javatar

pdfLink is never initialized (always null)?

Android Tags:android, kotlin, mutablelivedata, viewmodel

Post navigation

Previous Post: [FIXED] android – How to achieve ionic permissions on first load
Next Post: [FIXED] android – How do i put online database into local database

Related Posts

[FIXED] android – How to generate and publish Code Coverage in gradle kotlin using Azure DevOps? Android
[FIXED] android – How to set permanatly flutter sdk path in anroid studio? Android
[FIXED] java – Bluetooth low energy with android Android
[FIXED] android – Activity has leaked window DecorView while using alertDialog Android
[FIXED] java – I want to change image with blink animation in android and images are directly loading from server with Glide. or any other option to do this? Android
[FIXED] android – Webview images won’t load on real device Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme