list is not getting in recyclerview using retrofit

Deadly 提交于 2021-01-29 10:48:33

问题


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....


回答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.



来源:https://stackoverflow.com/questions/64365566/list-is-not-getting-in-recyclerview-using-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!