John Joe
John Joe

Reputation: 12803

ViewModel in recyclerView

I'm using ViewModel to load data and set it into SwipeRefreshLayout RecyclerView

    mAdapter = NewRecyclerViewAdapter(listAct, this@MainActivity, hashMap)
    list?.adapter = mAdapter

     workViewModel.getRequests()?.observe(
                this, Observer { resource ->
                    resource?.data?.let {
                        Log.d(TAG,it.size)
                        mAdapter.setOrders(it)
                    }
                }
            )

  workViewModel = ViewModelProviders.of(
            this,
            ViewModelFactory.getInstance(activity.application)
        ).get(WorkViewModel::class.java)
                   .......


swipe_container.setOnRefreshListener {
            mAdapter.setWorkOrders(listAct)
            mAdapter.notifyDataSetChanged()
        }    

I use the log to debug, it display size 2. Data seems successful to retrieve, but it did not set to the recyclerView.

Adapter

 private var order = emptyList<Order>()
     ......
     internal fun setOrders(order: List<Order>) {
            this.order = order
            this.order.sortedBy { it.created_at }
            notifyDataSetChanged()
        }

Upvotes: 0

Views: 441

Answers (2)

Happy Singh
Happy Singh

Reputation: 1482

You are setting the same list in your setOrders() functions Use this instead of your function

internal fun setOrders(orders: List<Order>) {
    this.order = orders.sortedBy { it.created_at }
    notifyDataSetChanged()
}

Upvotes: 1

apksherlock
apksherlock

Reputation: 8371

Are you using yourRecyclerView.setAdapter(mAdapter ) anywhere ? Or better : have you initialized the recyclerview and than setting the adapter? Case you are using databinding check your adapter atribute in the recyclerview xml tag

Upvotes: 0

Related Questions