如何在RecyclerView中更新多个文档

时间:2019-12-27 08:04:36

标签: android kotlin arraylist android-recyclerview indexoutofboundsexception

Screenshot

我正在尝试更新从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)

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

1 个答案:

答案 0 :(得分:0)

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和已解决