mvp 回收适配器不显示数据

mvp recycler adapter not showing data

我遇到的 RecyclerView 问题是数据来自服务器并且 API 响应在控制台中正确打印。

但是当我尝试在适配器中设置数据时出了什么问题,或者数据没有在 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
    }
}

我的持有人Class

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
}

同样,我在视图部分设置适配器是这样的:

//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)
}

在 Presenter Class 中,像这样从 Presenter 调用 setAdapter class

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)
        }
    }

我不明白这里出了什么问题。

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

您在 DashboardAdapter 上调用 showListItems() 作为类型而不是实例 dashboardAdapter。假设 dashboardAdapter 是本地 class 字段。

另外我想这种类型转换是不必要的,因为你已经在使用可选的 ? 所以,可以简化为:

dashboardAdapter?.showListItems(termlist!!, aboolean)

假设每当您检索 API 响应时都应调用它。因此,当有新的 API 数据时,必须调用 showList()