删除recyclerview项目时出错

时间:2018-02-27 20:32:54

标签: android arraylist android-recyclerview

我知道这是一个非常古老的问题,但即使在提到很多解决方案后,也无法解决我的问题。实际上,我试图删除longclick上的recyclerview项目,但我收到了以下错误。

顺便说一句,我在删除最后一项时没有收到任何错误,但是当我尝试删除除最后一项之外的任何其他项目时,我收到以下错误。< / p>

错误:

java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
                                                                    at java.util.ArrayList.remove(ArrayList.java:401)
                                                                    at community.infinity.message.MessageAdapter$1$1$1$1.run(MessageAdapter.java:219)
                                                                    at android.os.Handler.handleCallback(Handler.java:725)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                    at android.os.Looper.loop(Looper.java:137)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                    at java.lang.reflect.Method.invokeNative(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:511)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                    at dalvik.system.NativeStart.main(Native Method)

我的代码:

 @Override
 public void onBindViewHolder(ViewHolder holder, int position) {


 Message message = mMessages.get(position);
 holder.setMessage(message.getMessage());

 holder.msgContainer.setOnLongClickListener(view -> {
      int position = holder.getAdapterPosition();

 if (position != RecyclerView.NO_POSITION) {
 mMessages.remove(position);
 notifyItemRemoved(position);
 notifyItemRangeChanged(position, mMessages.size());
 }

        return false;
    });

 }

}
}

2 个答案:

答案 0 :(得分:3)

这里有两个问题。

首先,在致电notifyItemRemoved()后,无需致电notifyItemRemoved()。对holder.getAdapterPosition()的调用将正确调整其余数据集。

其次,NO_POSITION有时会返回-1(定义为notifyItemRemoved(holder.getAdapterPosition()))。当视图持有者知道它先前已绑定到不再位于适配器中的项目并且尚未重新绑定时,就会发生这种情况。由于您刚刚拨打-1,因此该持有人的适配器位置已被标记为无效,现在将以notifyItemRangeChanged()的形式返回。

如果您绝对必须致电int position = holder.getAdapterPosition(); if (position != RecyclerView.NO_POSITION) { mMessages.remove(position); notifyItemRemoved(position); notifyItemRangeChanged(position, mMessages.size()); } ,请按以下步骤重新编写代码:

webscap

答案 1 :(得分:0)

我个人曾经在

中的适配器之外(在片段/活动中)有点击监听器
    public void onBindViewHolder(ViewHolder holder, int position)
    {
        //I'm saving here the position of the current item
        holder.rootView.setTag(position);

        //you set the listener functionality from the fragment/activity by sending 
        //to the constructor of your adapter
        holder.setLongClickListener(mListener);

        //init other fields of your item
        ...
    }

    // a function that will responsible to update the list without the deleted item
    public void setData(List<Data> data)
    {
         mData = data;
         notifyDataSetChanged();
    }
}

<强>片段/活性

您的活动/片段应实现界面长按一下监听器

public class MyFragment extends Fragment implements View.LongClickListener
{
   ...

   public boolean onLongClick(View view) 
   {
         int selectedIndex = (int)view.getTag();
         mData.remove(selectedIndex);
         mAdapter.setData(mData);
         return true;
   }
}

不要忘记用这个初始化适配器,它需要你的onClickListener实现。

相关问题