我有一个带有R ecyclerview的活动,该活动使用一个接收ArrayList的自定义适配器,我需要删除一个单击TextView的特定列表项,问题是当我使用notifyItemRemoved(position)删除onBindViewHolder中的一个项时如果删除第一个元素,然后删除最后一个元素,则会收到IndexOutOfBoundsException。但是,如果我删除第一个到最后一个的所有元素,则可以正常工作。
这是我的完整课程代码:
class FichaDeContactoCompactaAdapter (private val context : Context, private val fichasContacto : ArrayList<FichaContacto>) : RecyclerView.Adapter<FichaDeContactoCompactaAdapter.ViewHolder>() {
inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v){
val titulo = v.findViewById<TextView>(R.id.tituloFichaContactoCompactaTV)
val editar = v.findViewById<TextView>(R.id.editarFCCTV)
val eliminar = v.findViewById<TextView>(R.id.eliminarFCCTV)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : ViewHolder{
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.list_item_ficha_contacto_compacta, parent, false)
return ViewHolder(v)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int){
holder.titulo.text = fichasContacto[position].titulo
holder.editar.setOnClickListener{
Toast.makeText(holder.editar.context, "Elementos en lista: " + fichasContacto.size + "IndiceAcutal: " + position , Toast.LENGTH_SHORT).show()
}
holder.eliminar.setOnClickListener{
Toast.makeText(holder.eliminar.context,"Indice eliminado:" + position + "Elementos en la lista: " + fichasContacto.size, Toast.LENGTH_SHORT).show()
fichasContacto.removeAt(position)
notifyItemRemoved(position)
}
}
override fun getItemCount(): Int {
return fichasContacto.size
}
}
启动的异常是:
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
我的筹码是:
2018-12-16 13:03:58.103 24131-24131/? E/adbd: recv: OPEN 00000048 00000000 0014:73 68 65 6C 6C 3A 6C 6F 67 63 61 74 20 2D 2D 68 65 6C 70 00
2018-12-16 13:03:58.167 24131-24131/? E/adbd: recv: OPEN 00000049 00000000 001e:73 68 65 6C 6C 3A 6C 6F 67 63 61 74 20 2D 76 20 6C 6F 6E 67 20 2D 76 20 65 70 6F 63 68 00
2018-12-16 13:03:58.178 3463-3463/? E/KeyguardFingerPrint: updateFingerprintListeningState#mFingerprintRunningState=0 shouldListenForFingerprint=false
2018-12-16 13:03:58.322 3463-3463/? E/KeyguardFingerPrint: updateFingerprintListeningState#mFingerprintRunningState=0 shouldListenForFingerprint=false
2018-12-16 13:03:58.425 2980-3416/? E/LightsService: Light requested not available on this device. 2
2018-12-16 13:03:58.487 24917-24917/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2018-12-16 13:03:58.568 3463-3463/? E/SystemUIImageView: set Image Drawable!!
2018-12-16 13:03:58.622 4355-4886/? E/PBSessionCacheImpl: sessionId[19155037527995828] not persisted.
2018-12-16 13:03:58.816 2980-2980/? E/MotionRecognitionService: mReceiver.onReceive : ACTION_USER_PRESENT :: UNLOCK SCREEN
2018-12-16 13:03:58.852 2980-2980/? E/MotionRecognitionService: mReceiver.onReceive : ACTION_USER_PRESENT :: UNLOCK SCREEN
2018-12-16 13:03:58.857 3463-3463/? E/KeyguardFingerPrint: updateFingerprintListeningState#mFingerprintRunningState=0 shouldListenForFingerprint=false
2018-12-16 13:03:58.868 2980-3001/? E/BatteryExternalStatsWorker: no controller energy info supplied for bluetooth
2018-12-16 13:03:58.959 2735-4113/? E/(FPLOG): 2.0.36.0 DeviceSetClock 0
2018-12-16 13:03:59.334 4421-4421/? E/ProActivieLaunch: Sending Broadcast
2018-12-16 13:03:59.345 2980-2980/? E/ActivityManager: TouchDown intent received, starting ActiveLaunch
2018-12-16 13:03:59.892 2980-3416/? E/LightsService: Light requested not available on this device. 2
2018-12-16 13:04:00.354 25029-25029/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2018-12-16 13:04:00.362 4421-4421/? E/ProActivieLaunch: Sending Broadcast
2018-12-16 13:04:00.367 2980-2980/? E/ActivityManager: TouchDown intent received, starting ActiveLaunch
2018-12-16 13:04:00.487 25044-25044/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2018-12-16 13:04:01.047 2980-3058/? E/WindowManager: win=Window{29aeb85 u0 com.sec.android.app.launcher/com.sec.android.app.launcher.activities.LauncherActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=false win.mRemoveOnExit=false win.mViewVisibility=8, caller=com.android.server.wm.AppWindowToken.destroySurfaces:748 com.android.server.wm.AppWindowToken.destroySurfaces:732 com.android.server.wm.WindowState.onExitAnimationDone:5523 com.android.server.wm.AppWindowAnimator.stepAnimationLocked:517 com.android.server.wm.AppWindowToken.stepAppWindowsAnimation:1745
2018-12-16 13:04:05.907 14047-14047/com.kps.spart.moskimedicationreminder E/ViewRootImpl: sendUserActionEvent() returned.
2018-12-16 13:04:06.075 2980-3058/? E/WindowManager: win=Window{ec9b3a2 u0 com.kps.spart.moskimedicationreminder/com.kps.spart.moskimedicationreminder.MainActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true win.mViewVisibility=0, caller=com.android.server.wm.AppWindowToken.destroySurfaces:748 com.android.server.wm.AppWindowToken.destroySurfaces:732 com.android.server.wm.WindowState.onExitAnimationDone:5523 com.android.server.wm.WindowStateAnimator.stepAnimationLocked:553 com.android.server.wm.DisplayContent.lambda$-com_android_server_wm_DisplayContent_21292:465
2018-12-16 13:04:06.334 2980-3562/? E/Watchdog: !@Sync 389 [2018-12-16 13:04:06.334]
2018-12-16 13:04:06.642 2980-4509/? E/ActivityThread: Failed to find provider info for com.sec.android.desktopmode.uiservice.SettingsProvider
2018-12-16 13:04:06.645 2980-4509/? E/InputMethodManagerService: Failed to get settings
2018-12-16 13:04:06.749 25089-25089/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2018-12-16 13:04:06.820 3445-3445/? E/TIPLib: [setActivateKeypad] The function setActivateKeypad is deprecated! Please, use setActiveKeypad.
2018-12-16 13:04:06.833 3445-3445/? E/SKBD: anw setInputMode, Cannot set Ambiguous or MultiTap with INPUT_QWERTY
2018-12-16 13:04:06.904 25089-25089/? E/[SPAF]AutofillService: No valid parsed autofill fields
2018-12-16 13:04:06.905 3445-3445/? E/SKBD: brg get previous
2018-12-16 13:04:06.909 3445-3445/? E/TIPLib: [setActivateKeypad] The function setActivateKeypad is deprecated! Please, use setActiveKeypad.
2018-12-16 13:04:06.913 3445-3445/? E/SKBD: anw setInputMode, Cannot set Ambiguous or MultiTap with INPUT_QWERTY
2018-12-16 13:04:06.953 2980-3058/? E/WindowManager: win=Window{54a5c0 u0 com.kps.spart.moskimedicationreminder/com.kps.spart.moskimedicationreminder.MainActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=false win.mRemoveOnExit=false win.mViewVisibility=8, caller=com.android.server.wm.AppWindowToken.destroySurfaces:748 com.android.server.wm.AppWindowToken.destroySurfaces:732 com.android.server.wm.WindowState.onExitAnimationDone:5523 com.android.server.wm.AppWindowAnimator.stepAnimationLocked:517 com.android.server.wm.AppWindowToken.stepAppWindowsAnimation:1745
2018-12-16 13:04:07.091 3445-3445/? E/SKBD: bbd [USICM][getSelectedText] updated, but null is returned
2018-12-16 13:04:07.921 3445-3445/? E/SKBD: bbw getInstance start
2018-12-16 13:04:07.921 3445-3445/? E/SKBD: bbw sendSIPInformation state: 6 isAbstractKeyboardView : true
2018-12-16 13:04:07.923 3445-25119/? E/SKBD: bbw sending null keyboardInfo as SIP is closed
2018-12-16 13:04:09.033 14047-14047/com.kps.spart.moskimedicationreminder E/ViewRootImpl: sendUserActionEvent() returned.
2018-12-16 13:04:09.142 2980-3058/? E/WindowManager: win=Window{f4b6005 u0 com.kps.spart.moskimedicationreminder/com.kps.spart.moskimedicationreminder.AnadirMedicoActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true win.mViewVisibility=0, caller=com.android.server.wm.AppWindowToken.destroySurfaces:748 com.android.server.wm.AppWindowToken.destroySurfaces:732 com.android.server.wm.WindowState.onExitAnimationDone:5523 com.android.server.wm.WindowStateAnimator.stepAnimationLocked:553 com.android.server.wm.DisplayContent.lambda$-com_android_server_wm_DisplayContent_21292:465
2018-12-16 13:04:09.365 25139-25139/? E/FeatureClassSet: [#CMH#] Rubin package not supported
2018-12-16 13:04:12.236 14047-14047/com.kps.spart.moskimedicationreminder E/ViewRootImpl: sendUserActionEvent() returned.
2018-12-16 13:04:12.349 2980-3058/? E/WindowManager: win=Window{eacd262 u0 com.kps.spart.moskimedicationreminder/com.kps.spart.moskimedicationreminder.AnadirMedicoActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true win.mViewVisibility=0, caller=com.android.server.wm.AppWindowToken.destroySurfaces:748 com.android.server.wm.AppWindowToken.destroySurfaces:732 com.android.server.wm.WindowState.onExitAnimationDone:5523 com.android.server.wm.WindowStateAnimator.stepAnimationLocked:553 com.android.server.wm.DisplayContent.lambda$-com_android_server_wm_DisplayContent_21292:465
2018-12-16 13:04:13.788 14047-14047/com.kps.spart.moskimedicationreminder E/ViewRootImpl: sendUserActionEvent() returned.
2018-12-16 13:04:13.885 2980-3058/? E/WindowManager: win=Window{c524a61 u0 com.kps.spart.moskimedicationreminder/com.kps.spart.moskimedicationreminder.AnadirMedicoActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true win.mViewVisibility=0, caller=com.android.server.wm.AppWindowToken.destroySurfaces:748 com.android.server.wm.AppWindowToken.destroySurfaces:732 com.android.server.wm.WindowState.onExitAnimationDone:5523 com.android.server.wm.WindowStateAnimator.stepAnimationLocked:553 com.android.server.wm.DisplayContent.lambda$-com_android_server_wm_DisplayContent_21292:465
2018-12-16 13:04:14.587 24952-25168/? E/BaseAppContext: Tried to stop global GMSCore RequestQueue. This is likely unintended, so ignoring.
2018-12-16 13:04:15.309 14047-14047/com.kps.spart.moskimedicationreminder E/ViewRootImpl: sendUserActionEvent() returned.
2018-12-16 13:04:15.420 2980-3058/? E/WindowManager: win=Window{cb23155 u0 com.kps.spart.moskimedicationreminder/com.kps.spart.moskimedicationreminder.AnadirMedicoActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true win.mViewVisibility=0, caller=com.android.server.wm.AppWindowToken.destroySurfaces:748 com.android.server.wm.AppWindowToken.destroySurfaces:732 com.android.server.wm.WindowState.onExitAnimationDone:5523 com.android.server.wm.WindowStateAnimator.stepAnimationLocked:553 com.android.server.wm.DisplayContent.lambda$-com_android_server_wm_DisplayContent_21292:465
2018-12-16 13:04:18.659 14047-14047/com.kps.spart.moskimedicationreminder E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kps.spart.moskimedicationreminder, PID: 14047
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.remove(ArrayList.java:503)
at com.kps.spart.moskimedicationreminder.FichaDeContactoCompactaAdapter$onBindViewHolder$2.onClick(FichaDeContactoCompactaAdapter.kt:36)
at android.view.View.performClick(View.java:6935)
at android.widget.TextView.performClick(TextView.java:12742)
at android.view.View$PerformClick.run(View.java:26211)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
2018-12-16 13:04:18.774 25185-25185/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2018-12-16 13:04:20.289 2980-4171/? E/KioskModeService: There's no matched data
2018-12-16 13:04:20.340 2980-4171/? E/KioskModeService: There's no matched data
2018-12-16 13:04:20.376 2980-4171/? E/ActivityManager: Found activity ActivityRecord{40e7eda u0 com.kps.spart.moskimedicationreminder/.MainActivity t-1 f} in proc activity list using null instead of expected ProcessRecord{f0882e 14047:com.kps.spart.moskimedicationreminder/u0a364}
2018-12-16 13:04:20.412 2980-2999/? E/ViewRootImpl@a76cbe7[moskimedicationreminder]: mStopped=false mHasWindowFocus=true mPausedForTransition=false
2018-12-16 13:04:20.414 2980-2999/? E/ViewRootImpl: sendUserActionEvent() returned.
2018-12-16 13:04:20.486 2980-3101/? E/InputDispatcher: channel ~ Channel is unrecoverably broken and will be disposed!
2018-12-16 13:04:20.630 4355-4886/? E/PBSessionCacheImpl: sessionId[19155037527995828] not persisted.
2018-12-16 13:04:20.735 2980-3058/? E/WindowManager: win=Window{93c86c6 u0 com.kps.spart.moskimedicationreminder/com.kps.spart.moskimedicationreminder.AnadirMedicoActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true win.mViewVisibility=0, caller=com.android.server.wm.AppWindowToken.destroySurfaces:748 com.android.server.wm.AppWindowToken.destroySurfaces:732 com.android.server.wm.WindowState.onExitAnimationDone:5523 com.android.server.wm.AppWindowAnimator.stepAnimationLocked:517 com.android.server.wm.TaskStack.stepAppWindowsAnimation:2018
2018-12-16 13:04:24.471 3430-3844/? E/ImsAdaptorImpl: setSSACInfo : ImsAdaptorImpl.
2018-12-16 13:04:24.487 3430-3844/? E/ImsAdaptorImpl: setSSACInfo : ImsAdaptorImpl.
2018-12-16 13:04:24.588 2980-2980/? E/WifiGeofenceManager: setScanInterval interval : 32, maxInterval : 128
2018-12-16 13:04:24.596 2980-2980/? E/WifiGeofenceManager: setScanInterval interval : 32, maxInterval : 128
答案 0 :(得分:0)
也许您需要记录特定阵列列表 Log.e(“ data”,fishcazContacto.toString)
答案 1 :(得分:0)
解决:
holder.eliminar.setOnClickListener{
fichasContacto.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(position, fichasContacto.size)
}