RecyclerView和IndexOutOfBoundsException:无效的索引#,大小为# [英] RecyclerView and IndexOutOfBoundsException: Invalid index #, size is #

查看:119
本文介绍了RecyclerView和IndexOutOfBoundsException:无效的索引#,大小为#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的RecyclerView,我想在onClick上删除这些项目.根据我何时单击列表中出现上述错误的某些项目,我一直在崩溃.如果您遇到类似的问题,请按照以下方法解决:

I have a simple RecyclerView and I want to remove those items onClick. I was continuously running into a crash depending on when I clicked on certain items in the list with the above error. If you're having similar issues, here's how I solved it:

推荐答案

public void onBindViewHolder(final CardViewHolder holder, final int position)

在某些情况下,使用此处的位置很有用,但是如果我在从列表中删除项目时使用了位置,则会导致崩溃,而不是使用:

Using position here is useful for some things, but if I used it when removing items from the list, it was causing a crash, instead using:

holder.getAdapterPosition();

立即解决了我的问题.

这篇关于RecyclerView和IndexOutOfBoundsException:无效的索引#,大小为#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆