list is not getting in recyclerview using retrofit
Asked Answered
L

1

0

basically im trying display list in recyclerview using retrofit and viewmodel........

on debugging the onresponse im getting 200 response but why is it not displaying list in recyclerview i dont know where im going wrong

i will post up more codes if needed

following is the code:---

class Tables : BaseClassActivity() {
lateinit var recyclerView: RecyclerView
lateinit var recyclerAdapter: TableAdapter

 var Tablelist : MutableList<Tabledata> = mutableListOf()
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.table_activity)
    var mActionBarToolbar = findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbartable);
    setSupportActionBar(mActionBarToolbar);
    setScreenTitle("Tables")

    recyclerView = findViewById(R.id.recyleview)

    val model = ViewModelProviders.of(this)[HeroViewModel::class.java]

    model.heroes?.observe(this,object :Observer<Table_response>{
        override fun onChanged(t: Table_response?) {
            recyclerAdapter = TableAdapter(applicationContext, Tablelist)
            recyleview.layoutManager = LinearLayoutManager(applicationContext)
            recyclerView.addItemDecoration(
                DividerItemDecoration(
                    recyclerView.context,
                    DividerItemDecoration.VERTICAL
                )
            )
            recyleview.adapter = recyclerAdapter            }

    })

    }

HeroViewModel:--

class HeroViewModel : ViewModel() {
var recyclerAdapter: TableAdapter?=null

private var heroList: MutableLiveData<Table_response>? = null
val heroes: MutableLiveData<Table_response>?
    get() {
        //if the list is null
        if (heroList == null) {
            heroList = MutableLiveData<Table_response>()
            //we will load it asynchronously from server in this method
            loadHeroes()
        }

        //finally we will return the list
        return heroList
    }

//This method is using Retrofit to get the JSON data from URL
private fun loadHeroes() {

    RetrofitClient.instance.getAllPhotos(product_category_id = "1", value = 1).enqueue(
        object : Callback<Table_response> {
            override fun onFailure(call: Call<Table_response>, t: Throwable) {

            }

            override fun onResponse(
                call: Call<Table_response>,
                response: Response<Table_response>
            ) {

                if (response.body() != null) {
                    val res = response
                    heroList!!.value = response.body()

                    recyclerAdapter?.setMovieListItems((response.body()?.data as MutableList<Tabledata>?)!!)
                }
            }

        })
}

}

need help thanks....

Lacking answered 15/10, 2020 at 5:48 Comment(0)
F
1

You are passing empty list every time you have onChanged() callback in your Activity, and you are trying to set the response on TableAdapter from ViewModel that is never created. You shouldn't do this, what you should do is you should move this code:

recyclerAdapter?.setMovieListItems((response.body()?.data as MutableList<Tabledata>?)!!)

in here:

model.heroes?.observe(this,object :Observer<Table_response>{
        override fun onChanged(t: Table_response?) {
            recyclerAdapter = TableAdapter(applicationContext, Tablelist)
            recyleview.layoutManager = LinearLayoutManager(applicationContext)
            recyclerView.addItemDecoration(
                DividerItemDecoration(
                    recyclerView.context,
                    DividerItemDecoration.VERTICAL
                )
            )
            
            recyclerAdapter.setMovieListItems(t?.data as MutableList<Tabledata>?)
            recyleview.adapter = recyclerAdapter            }

    })

And remove the adapter from ViewModel.

Filippo answered 15/10, 2020 at 6:0 Comment(7)
im getting error in response variable response.body()?.data as MutableList<Tabledata> if i move this to onchangedLacking
hey can you help me here--> #64353252Lacking
Hey, I think that the code posted there is good, however OP haven't posted the dependencies used for the answer. I left the comment, and you can try with the code posted there, but try adding those dependencies: implementation "androidx.activity:activity-ktx:1.1.0Filippo
Actucally i was trying to make login using the code which i have posted above..like making a use of viewmodelprovider.of the answer which is posted there is totally complex...i am confused like how do i make that login using the above codeLacking
Let's take the discussion thereFilippo
@IRONMAN I gave my 5 cents on that there: https://mcmap.net/q/540224/-user-login-using-viewmodel-and-retrofitFilippo
need help here --> #65425008Lacking

© 2022 - 2024 — McMap. All rights reserved.