Hubby Tiwari
Hubby Tiwari

Reputation: 363

mvp recycler adapter not showing data

The problem I am facing with the RecyclerView is the data is coming from Server and the API response is getting printed correctly in the console.

but when I am trying to set data in the adapter what is wrong or something is not going correctly with the flow that the data is not being updated on UI.

//This is my adapter class

class DashboardAdapter(val context: Context) : RecyclerView.Adapter<DashBoardHolder>() {
    private var transactionList = ArrayList<DashboardData>()
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DashBoardHolder {
        val inflator = LayoutInflater.from(parent.context)
        val view = ActivityDashboardDataBinding.inflate(inflator, parent, false)
        val viewHolder = DashBoardHolder(view)
        return viewHolder
    }

    override fun onBindViewHolder(holder: DashBoardHolder, position: Int) {
        val quickModel = transactionList[position]
        holder.tvName.text = quickModel.bookingTitle
    }

    fun showListItems(dashboardlist: List<DashboardData>?, aboolean: Boolean) {
        when {
            aboolean -> transactionList.clear()
        }
        if (dashboardlist != null && !dashboardlist.isEmpty())
            this.transactionList.addAll(dashboardlist)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return transactionList.size
    }
}

MyHolderClass

class DashBoardHolder(val binding: ActivityDashboardDataBinding) :
    RecyclerView.ViewHolder(binding.root) {
    var tvName: TextView = binding.textViewGrandrukName
    var tvTime: TextView = binding.tvGrandrukTripDetails
    var tvPlace: ImageView = binding.btnGhandruk
    var ivRectangle: ImageView = binding.imageView5
}

Similarly,I set the adapter in view section like this way:

//setting adapter in Presenter class
fun setAdapter() {
    var layoutmanager: LinearLayoutManager? = LinearLayoutManager(appCompatActivity)
    val firstVisiblePosition = layoutmanager!!.findFirstVisibleItemPosition()
    binding!!.includesDashboardRecyclerview.rvBookingList.setHasFixedSize(true)
    binding!!.includesDashboardRecyclerview.rvBookingList.layoutManager = layoutmanager  
    binding!!.includesDashboardRecyclerview.rvBookingList.adapter = dashboardAdapter  
    layoutmanager!!.scrollToPositionWithOffset(firstVisiblePosition, 0)
}

In Presenter Class, calling setAdapter class from presenter like this way

class DashboardPresenter(
        private val dashboardView: DashboardView,
        private val dashboardModel: DashboardModel
    ) {

        fun onCreateView() {
            onClick()
            dashboardView.setAdapter()
            getDashboardRequest()
        }

        //calling adpter function here
        fun showList(termlist: List<DashboardData>?, aboolean: Boolean) {
            (null as DashboardAdapter?)?.showListItems(termlist!!, aboolean)
        }
    }

I'm not able to understand what is getting wrong here.

Upvotes: 1

Views: 118

Answers (1)

Zain
Zain

Reputation: 40840

(null as DashboardAdapter?)?.showListItems(termlist!!, aboolean)

You are calling showListItems() on the DashboardAdapter as a type not the instance dashboardAdapter. Assuming that dashboardAdapter is a local class field.

Also I guess this type casting is not necessary as you already using the optional ? So, it can be simplified to:

dashboardAdapter?.showListItems(termlist!!, aboolean)

Assuming that this should be called whenever you retrieve the API response. So, showList() must be called when there's new API data.

Upvotes: 2

Related Questions