3

i am new in android, i am working on an app that retrieve data from server using retrofit and kodein and MvvM in kotlin i set a Navigation drawer in my app and the purpose is that when i click on the item of navigation drawer new activity opens and in this activity i want show recyclerview but when new activty opens recyclerview cant set listitem on recycler i debug my code in my repositories and viewmodel class and i see that they received data i debug my code in new activty and i see that viewmodel cant receive those data and set a invalid icon next to my code in viewmodel.observe

this is my repository class:

 fun getdigitalproduct(): LiveData<List<DigitalProduct>>{
    val dpData:MutableLiveData<List<DigitalProduct>> = MutableLiveData<List<DigitalProduct>>()
    val apiClient = ApiClient()

    val call:Call<List<DigitalProduct>> = apiClient.getClient().create(ApiService::class.java).getdigitalproduct()
    call.enqueue(object : Callback<List<DigitalProduct>>{
        override fun onResponse(
            call: Call<List<DigitalProduct>>,
            response: Response<List<DigitalProduct>>
        ) {
            dpData.value = response.body()
        }

        override fun onFailure(call: Call<List<DigitalProduct>>, t: Throwable) {
             dpData.value = null
        }

    })
    return dpData
}

this is for ViewModel

var repoDigitalProduct: LiveData<List<DigitalProduct>> = repositorys.getdigitalproduct()
fun getdigitalproduct(): LiveData<List<DigitalProduct>>{
    return repoDigitalProduct
 }

this is for new activity:

  private fun getDigitalProduct() {
    viewModel.getdigitalproduct().observe(this, Observer {
        digipro.addAll(it)
    })

this is digipro:

var digipro: ArrayList<DigitalProduct> = ArrayList()

and this i use this code in oncreate method in new activity:

  viewModel = ViewModelProviders.of(this, factory).get(AllViewModel::class.java)
    getDigitalProduct()
    setdigitalProductRecycler()

i use viewmodelprovider.of code in Mainactivity too

this is for setdigitalProductRecycler:

 private fun setdigitalProductRecycler() {
    val digiproRecycler = digital_product_recycler
    digiproRecycler.layoutManager =
        LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true)
    digiproRecycler.adapter = DigitalProductAdapter(digipro)
}

my codes works in Mainactivity but when i try it in new activity.........

what should i do?

Hassan
  • 380
  • 6
  • 20

2 Answers2

2

The best way to solve this issue is create your viewmodel in activity or you can use shared viewmodel. So your viewmodel will retain as your activity retain

https://stackoverflow.com/a/52611554/8868638

Yehezkiel L
  • 389
  • 2
  • 10
  • 1
    i used shared viewmodel before but it didnt worked for me now i understand that i should set my recycler in viewmodel.observe method now its fine – Hassan Dec 24 '20 at 08:57
1

i put repositorie and viewmodel in companion object then i called viewmodel of MainActivity in that activity

this is for MainActivty:

 companion object{
   val repositorys = AllRepositorys()
   var viewModel: AllViewModel = AllViewModel(repositorys)
}

this is for newActivity:

lateinit var viewModel: AllViewModel

and i put this in oncreate() Method in new activity:

 viewModel = MainActivity.viewModel

UPDATE

Now I handle this issue with creating a viewmodel for every fragment or activity. Because, ViewModel is designed to store and manage UI-related data in a lifecycle of Activity or Fragment

Hassan
  • 380
  • 6
  • 20
  • this is not the proper solution, because your viewmodel and repository will remain in memory since you create it in companion object – Yehezkiel L Dec 11 '20 at 09:36