首页 > 解决方案 > 如何在 RecyclerView 中更新多个文档

问题描述

截屏

我正在尝试更新从 fileManager 中选择的文档,我已将列表中的所有文档显示为recycler view.

  override fun onBindViewHolder(holder: IndianPatientFormSixViewHolder, position: Int) {

    val docs = viewModel?.patientDocumentList?.get(position)

    if(viewModel?.positionOfDocumentSubmittedList?.isNotEmpty() == true){

        val submittedDocPosition = viewModel?.positionOfDocumentSubmittedList?.get(position)

        if(submittedDocPosition == position){
            holder.docUploadedText.visibility = View.VISIBLE
        } else {
            holder.docUploadedText.visibility = View.GONE
        }
    } else {
        holder.docUploadedText.visibility = View.GONE
    }

    holder.certificateNameTextView.text = docs?.label

    holder.uploadImage.setOnClickListener {
        indianPatientFormSixCallback?.onUploadButtonClicked(position,itemId = docs?.doc_id ?: 0)
    }

}

以上是我的代码

我有两个列表patientDocumentList,它们来自具有文档列表的 API,并且我已经为选定的文档位置定义了positionOfDocumentSubmittedList

如果我在更新回收站视图的位置时单击文档列表的第二位置,则会出现以下错误:

    java.lang.IndexOutOfBoundsException: Index: 3, Size: 1
    at java.util.ArrayList.get(ArrayList.java:437)
    at com.pravin_agarwal_foundation.tpaf.ui.funding_request_patient.indian.indian_patient_form_six.IndianPatientFormSixAdapter.onBindViewHolder(IndianPatientFormSixAdapter.kt:42)
    at com.pravin_agarwal_foundation.tpaf.ui.funding_request_patient.indian.indian_patient_form_six.IndianPatientFormSixAdapter.onBindViewHolder(IndianPatientFormSixAdapter.kt:14)
    at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
    at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
    at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
    at android.view.View.layout(View.java:20703)
    at android.view.ViewGroup.layout(ViewGroup.java:6198)
    at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
    at android.view.View.layout(View.java:20703)
    at android.view.ViewGroup.layout(ViewGroup.java:6198)
    at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
    at android.view.View.layout(View.java:20703)
    at android.view.ViewGroup.layout(ViewGroup.java:6198)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20703)
    at android.view.ViewGroup.layout(ViewGroup.java:6198)
    at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)

我还附上了更新的回收站视图的屏幕截图。

标签: androidkotlinarraylistandroid-recyclerviewindexoutofboundsexception

解决方案


val listSize = viewModel?.positionOfDocumentSubmittedList

if(viewModel?.positionOfDocumentSubmittedList?.isNotEmpty() == true){

    for (i in 0 until listSize!!.size) {

        val submittedDocPosition = viewModel?.positionOfDocumentSubmittedList?.get(position)
        if (submittedDocPosition == position) {
            holder.docUploadedText.visibility = View.VISIBLE
        } else {
            holder.docUploadedText.visibility = View.GONE
        }
    }
} else {
    holder.docUploadedText.visibility = View.GONE
}

我已经为 SubmittedDocumentList 和 Solved 使用了循环


推荐阅读